ubuntu12.04預設已經安裝了QT
建立一個檔案夾
建立檔案helloworld.cpp
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton hello("Hello Ubuntu!");
hello.resize(, );
hello.show();
return app.exec();
}
儲存。
生成qt工程檔案
qmake -project //生成helloworld.pro
qmake //生成makefile
make //生成可執行檔案
編譯時,終端的輸出為:
[email protected]:~/qt4/qt4helloworld$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++- -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o helloworld.o helloworld.cpp
g++ -m64 -Wl,-O1 -o qt4helloworld helloworld.o -L/usr/lib/x86_64-linux-gnu -lQtGui -lQtCore -lpthread
QT 中如何使用 cin cout ?
QString 比 string 更加強大的,記憶體管理
#include <QString>
#include <iostream>
#include <string>
#include <QByteArray>
#include <QDebug>
using namespace std;
int main(int argc, char *argv[])
{
QString str = "welcome ";
str += "to you ";
//1
std::cout << str.toStdString().data() << endl;
//2
qDebug() << str;
//3.append
QString str2 = "LIMAO";
str.append(str2);
cout << str.toStdString().data() << endl;
//4.sprintf 組合字元串
QString str3;
str3.sprintf("%s"," you are great ! ");
str3.sprintf("%s -- %s","you are great","limao");
cout << str3.toStdString().data() << endl;
//5.arg 方法
str3 = QString("%1 was born in %2.").arg("LM").arg();
cout << str3.toStdString().data() << endl;
//6.判斷
bool s = str3.startsWith("LM",Qt::CaseSensitive);
if (s)
cout << "yes" << endl;
//7.包含
bool ss = str3.contains("M",Qt::CaseSensitive);
if(ss)
cout << "yes" << endl;
//8.轉換
bool ok;
QString sss = "123";
QString sss2 = "123.456";
int dec = sss.toInt(&ok,);
double decd = sss2.toDouble(&ok);
cout << sss.toStdString().data() << dec << endl;
int dec2 = sss.toInt((&ok));
cout << sss2.toStdString().data() << decd << endl;
//QByteArray 類似 const char*,增加了隐式轉換
//QByteArray ba = str3.toAscii() QT5 改成了toLatin1()
QByteArray ba = str3.toLatin1();
ba.append("---9---");
cout << ba.data() << endl;
qDebug() << ba.data();
return ;
}
QList 容器
#include <QByteArray>
#include <QDebug>
using namespace std;
int main(int argc, char *argv[])
{
// QList<int> list;
// list << 1 << 2 << 3 << 4 << 5;
// QListIterator<int> i(list);
// for(i;i.hasNext();)
// qDebug() << i.next() ;
QList<int> list2;
QMutableListIterator<int> ii(list2);
for(int j = ; j<;++j)
{
ii.insert(j);
}
for(ii.toFront();ii.hasNext();)
qDebug() << ii.next();
for(ii.toBack();ii.hasPrevious();)
{
if(ii.previous()% == )
ii.remove();
else
ii.setValue(ii.peekNext()*);
}
for(ii.toFront();ii.hasNext();)
qDebug() << ii.next();
return ;
}
QMap 鍵值對容器
#include <QString>
#include <iostream>
#include <string>
#include <QByteArray>
#include <QDebug>
using namespace std;
int main(int argc, char *argv[])
{
QMap<QString,QString> map;
map.insert("beijing","010");
map.insert("shanghai","021");
map.insert("nanjing","055");
QMapIterator<QString,QString> i(map);
for(;i.hasNext();)
qDebug() <<" " << i.key() <<" "<< i.next().value() ;
QMutableMapIterator<QString,QString> mi(map);
if(mi.findNext("055"))
{
mi.setValue("1000");
cout << "yes" << endl;
}
return ;
}
.pro 的工程檔案都有什麼:
QT += core
QT -= gui
CONFIG += c++11
TARGET = qstring
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
QtAlgorithms 和 QtGlobal 中提供了一些算法和函數
#include <QDebug>
int main(int argc,char *argv[])
{
double a=-,b=;
double c=qAbs(a); //c=19.3
double max=qMax(b,c); //max=c=19.3
int bn=qRound(b); //bn=10
int cn=qRound(c); //cn=19
qDebug()<<"a="<<a;
qDebug()<<"b="<<b;
qDebug()<<"c=qAbs(a)= "<<c;
qDebug()<<"qMax(b,c)= "<<max;
qDebug()<<"bn=qRound(b)= "<<bn;
qDebug()<<"cn=qRound(c)= "<<cn;
qSwap(bn,cn);
qDebug()<<"qSwap(bn,cn) "<<"bn="<<bn<<" cn="<<cn;
return ;
}
.pro檔案如下
QT += core
QT -= gui
TARGET = Algorithms
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
補充一個模闆的pro檔案(pro生成makefile,屏蔽了作業系統的差别)
QT += core gui
greaterThan(QT_MAJOR_VERSION,): QT += widgets
TARGET = helloworld #生成的可執行檔案名
TEMPLATE = app
INCLUDEPATH += E:/BCC/include \
E:/VC/include
SOURCES += Main.cpp \
helloworld.cpp
HEADERS += helloworld.h \
FORMS += helloworld.ui
RC_FILE += myico.rc
LIBS += -LE:vcc/lib
CONFIG += warn_on debug
CONFIG(debug){
DEFINES += DEBUG_LOG #定義一個宏
SOURCES += Debuglog.cpp #增加兩個檔案到該工程
HEADERS += Debuglog.h
}
執行個體(D.T.Tang):
放在main中主程式
#include <QWidget>
#include <QApplication>
#include <QtGui>
#include <QLineEdit>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget* w = new QWidget(NULL,Qt::WindowCloseButtonHint|Qt::WindowCancelButtonHint);
QLineEdit* le = new QLineEdit(w);
QPushButton* button[] = {};
const char* str[] = {
"7","8","9","+","(",
"4","5","6","-",")",
"1","2","3","*","<-",
"0",".","=","/","C"
};
int ret = ;
le->move(,);
le->resize(,);
le->setReadOnly(true);
\
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
button[i* + j] = new QPushButton(w);
button[i* + j]->resize(,);
button[i* + j]->move( + ( + )*j , +( + )*i);
button[i* + j]->setText(str[i* + j]);
}
}
w->show(); //show window
w->setFixedSize(w->width(),w->height());
ret = a.exec(); //message polling
delete w; // 不del 會記憶體洩漏
return ret;
}
重新構造refactoring 模式生成該界面,使用了二階構造方法,主要防止在動态配置設定資源失敗的情況。因為構造函數沒有傳回值,構造失敗了,你也不知道。是以在正常構造函數中普通成員,new等方法在二階構造函數中完成(可以為private),直接調用接口直接生成一個本對象的執行個體指針(并在其中調用二階構造函數,靜态static,傳回本對象指針)
參考:
http://blog.csdn.net/qq_29344757/article/details/
重構該計算機執行個體
QCalculatorUI.h
#ifndef QCALCULATORUI_H
#define QCALCULATORUI_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
class QCalculatorUI : public QWidget
{
Q_OBJECT //因為你自定義了槽
private:
QLineEdit* m_edit;
QPushButton* m_buttons[];
QCalculatorUI();
bool construct();
public:
~QCalculatorUI();
static QCalculatorUI* NEW();
void show();
private slots: //聲明自定義槽的方法
void onbtnclicked();
};
#endif // QCALCULATORUI_H
QCalculatorUI.cpp
#include "QCalculatorUI.h"
QCalculatorUI::QCalculatorUI() : QWidget(NULL,Qt::WindowCloseButtonHint)
{
}
QCalculatorUI::~QCalculatorUI()
{
}
bool QCalculatorUI::construct()
{
bool ret = true;
const char* str[] = {
"7","8","9","+","(",
"4","5","6","-",")",
"1","2","3","*","<-",
"0",".","=","/","C"
};
m_edit = new QLineEdit(this);
if(m_edit != NULL)
{
m_edit->move(,);
m_edit->resize(,);
m_edit->setReadOnly(true);
}
else
{
return false;
}
for(int i = ; (i<) && ret; i++)
{
for(int j = ; (j<) && ret; j++)
{
m_buttons[i* + j] = new QPushButton(this);
if(m_buttons[i* + j] != NULL)
{
m_buttons[i* + j]->resize(,);
m_buttons[i* + j]->move( + ( + )*j , +( + )*i);
m_buttons[i* + j]->setText(str[i* + j]);
}
else
{
return false;
}
}
}
return ret;
}
//erjie gouzaofa
QCalculatorUI* QCalculatorUI::NEW()
{
QCalculatorUI* ret = new QCalculatorUI();
if( (ret == NULL) || (!ret->construct()) )
{
delete ret;
ret = NULL;
}
return ret;
}
void QCalculatorUI::show() //自定義槽,其實和普通函數一樣
{
QWidget::show();
QWidget::setFixedSize(width(),height());
}
main.cpp
#include "QCalculatorUI.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCalculatorUI* cal = QCalculatorUI::NEW();
int ret = -;
if(cal != NULL)
{
cal->show();
ret = a.exec();
delete cal;
}
return ret;
}