原文:http://blog.csdn.net/dipperkun/article/details/6266103
以命令行的形式改變窗口的現實風格
./xxx -style cde/motif/plastique
根據標籤快捷鍵定位輸入框
QLabel *label = new QLabel(tr("&New"));
QLineEdit *edit = new QLineEdit();
label->setBubby(edit);
默認按鈕:當用戶按下Enter的時候,能夠按下對應的按鈕
button->setDefault(true);
禁止按鈕:顯示爲灰色,不和用戶交互
button->setEnabled(false);
佈局中佔用其他的剩餘空間
layout->addStretch();
設置窗口的固定高度和寬度
dlg->setFixedHeight(dlg->sizeHint().height());
dlg->setFixedWidth(dlg->sizeHint().width());
信號與槽:
- 一個信號可以連接多個槽
- 多個信號可以連接同一個槽
- 一個信號可以與另一個信號連接
- 連接可以被移除(disconnect)
- 信號的參數必須和槽的參數相匹配,參數個數可以多於槽的參數個數,多餘的參數將被忽略
利用Qt設計師生成窗口
生成文件dlg.ui;
繼承Ui::Dlg.
- //dlg.h
- #include "ui_dlg.h"
- class Dlg : public QDialog, public Ui::Dlg
- {
- Q_OBJECT
- public:
- Dlg(QWidget *parent = 0);
- ...
- private slots:
- void on_lineEdit_textChanged();
- };
- //dlg.cpp
- #include "dlg.h"
- Dlg::Dlg(QWidget *parent)
- : QDialog(parent)
- {
- setupUi(this); // Notice!
- QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
- lineEdit->setValidator(new
- QRegExpValidator(regExp, this));
- ...
- }
- void Dlg::on_lineEdit_textChanged()
- {
- okBtn->setEnabled(lineEdit->hasAcceptableInput());
- }
- ...
- ...
自動連接信號與槽
setupUi函數會自動將符合on_objectName_signalName()命名慣例的任意槽
與相應的objectName的signalName()信號連接到一起。上例中,建立了下面
的信號-槽關係:
connect(lineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(on_lineEdit_textChanged()));
刪除父對象時,它的子對象會被自動刪除
通過佈局管理器來管理擴展對話框的窗口大小
layout()->setSizeConstraint(QLayout::SetFixedSize);
設計一個可擴展對話框的基本思路:
- 切換按鈕
- 信號-槽連接
- 不可以改變尺寸大小的佈局
多頁窗口部件有:QTabWidget, QToolBox
項目視圖窗口部件(帶滾動條):QListView, QTreeView, QTableView
爲應用程序提供圖片的方法:
- 把圖片保存在文件中,並且在運行時載入它們
- 把XPM文件包含在源代碼中
- 使用Qt的資源管理機制
使用資源管理機制的方法:
- 將圖片放在images/目錄下;
- 在xxx.pro加入:RESOURCES = xxx.qrc
- 增加資源文件xxx.qrc, 內容形式如下:
- <!DOCTYPE RCC>
- <RCC version="1.0">
- <qresource>
- <file alias="title.png">images/icon.png</file>
- ...
- <file>images/abc.png</file>
- </qresource>
- </RC
源代碼中引用方式:setWindowIcon(QIcon(":/images/icon.png"));
或者 setWindowIcon(QIcon(":/title.png"));
創建菜單欄、工具欄、狀態欄
- void MainWin::createActions()
- {
- newAct = QAction(tr("&New"), this); // 加速鍵N
- newAct->setIcon(QIcon("images/new.png")); // 圖標
- newAct->setShortcut(QKeySequence::New); // 快捷鍵 Ctrl+N
- // newAct->setShortcut("Ctrl+N"); // 快捷鍵 Ctrl+N
- newAct->setStatusTip(tr("Create a new file")); // 狀態提示
- connect(newAct, SIGNAL(triggered()), this, newFile());
- ...
- showGridAct->setCheckable(true); // 帶複選框的菜單
- showGridAct->setChecked(true); // 選中
- ...
- }
- // 菜單欄
- void MainWin::createMenus()
- {
- fileMenu = menuBar()->addMenu(tr("&File")); // file菜單
- fileMenu->addAction(newAct); // 添加到菜單中
- ...
- //QAction *separatorAct;
- fileMenu->addSeparator(); // 添加間隔器
- ...
- editMenu = menuBar()->addMenu(tr("&Edit")); // edit菜單
- QMenu *subMenu = editMenu->addMenu(tr("&Select")); // 添加子菜單
- subMenu->addAction(...);
- ...
- }
- // 工具欄
- void MainWin::createToolBars()
- {
- fileToolBar = addToolBar(tr("&File"));
- fileToolBar->addAction(newAct);
- ...
- editToolBar = addToolBar(tr("&Edit"));
- editToolBar = addAction(...);
- editToolBar = addSeparator();
- ...
- }
- // 狀態欄
- void MainWin::createStatusBar()
- {
- locationLabel = new QLabel(tr(" W999 "));
- locationLabel->setAlignment(Qt::AlignHCenter);
- locationLabel->setMinimumSize(locationLabel->sizeHint());
- otherLabel = new QLabel;
- otherLabel->setIndent(3); // 縮進3個字符
- statusBar()->addWidget(locationLabel);
- statusBar()->addWidget(otherLabel, 1); // 窗口改變時,伸展它
- }
模態對話框與非模態對話框
模態對話框典型例子:打開文件對話框,警告對話框
非模態對話框典型例子:查找對話框
模態對話框一般在堆中創建,非模態對話框一般在棧中創建
模態對話框使用exec()顯示,非模態對話框使用show()顯示
- void MainWin::find()
- {
- if (!findDlg) // 不存在,創建它
- {
- findDlg = new FindDlg(this);
- }
- findDlg->show(); // 顯示,並且是非模態的
- findDlg->raise(); // 位於最上方
- findDlg->activateWindow(); // 激活
- }
- void MainWin::goTo()
- {
- GoToDlg dlg(this);
- if (dlg.exec()) // 模態的
- {
- // 對話框返回true(QDialog::Accepted)
- ...
- }
- // 函數結束時,自動銷燬對話框
- }
創建一個啓動畫面
- int main(...)
- {
- QApplication app(...);
- QSplashScreen *splash = new QSplashScreen;
- splash->setPixmap(QPixmap(":/images/splash.png"));
- splash->show();
- app.processEvents(); // 處理點擊啓動畫面的事件
- splash->showMessage(QObject::tr("XXXX YYYYY ..."),
- Qt::AlignRight|Qt::AlignTop, Qt::white);
- MainWin win;
- splash->showMessage(...);
- initNetwork(...);
- ...
- win.show();
- splash->finish(&win);
- delete splash;
- return app.exec();
- }
MainWindow的中央窗口部件可以爲:
- 一個標準的Qt窗口部件
- 一個自定義的窗口部件
- 一個帶佈局管理器的普通QWidget
- 一個切分窗口(QSplitter)
- 一個多文檔工作空間(QMdiArea)
讀寫平臺無關的二進制文件
- bool writeFile(QString &fileName)
- {
- QFile file(fileName);
- if (!file.open(QIODevice::WriteOnly))
- {
- QMessageBox::warning(this, tr(""), tr("Can not write file %1:/n%2")
- .arg(file.fileName()).arg(file.errorString()));
- return false;
- }
- QDataStream out(&file);
- out.setVersion(QDataStream::Qt_4_6);
- out << quint32(0X11223344);
- QApplication::setOverrideCursor(Qt::WaitCursor);
- out << quint8(x) << qint32(y) << QString(str);
- out << ...
- ...
- QApplication::restoreOverrideCursor();
- return true;
- }
- bool readFile(QString &fileName)
- {
- QFile file(fileName);
- if (!file.open(QIODevice::ReadOnly))
- {
- QMessageBox::warning(this, tr(""), tr("Can not read file %1:/n%2")
- .arg(file.fileName()).arg(file.errorString()));
- return false;
- }
- QDataStream in(&file);
- in.setVersion(QDataStream::Qt_4_6);
- quint32 magic;
- in >> magic;
- if (magic != 0x11223344)
- {
- QMessageBox::warning(this, tr(""), tr("The file is not a xxx file."));
- return false;
- }
- QApplication::setOverrideCursor(Qt::WaitCursor);
- quint8 x;
- qint32 y;
- QString str;
- while (!in.end())
- {
- in >> x >> y >> str;
- in >> ...
- ...
- }
- QApplication::restoreOverrideCursor();
- return true;
- }
創建一個自定義窗口部件的過程:
- 選擇一個合適的窗口部件
- 對它子類化
- 實現虛函數,改變它的行爲
構建一個可以集成到Qt設計師中的窗口部件
- // iconeditor.h
- #ifndef ICONEDITOR_H
- #define ICONEDITOR_H
- #include <QColor>
- #include <QImage>
- #include <QWidget>
- class IconEditor : public QWidget
- {
- Q_OBJECT // 必須
- //設計師的屬性編輯器顯示這些自定義屬性
- Q_PROPERTY(QImage iconImage READ iconImage WRITE setIconImage)
- Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setZoomFactor)
- public:
- IconEditor(QWidget *parent = 0);
- void setIconImage(const QImage ℑ);
- QImage iconImage() const { return image; }
- void setZoomFactor(int zoom);
- int zoomFactor() const { return zoom; }
- QSize sizeHint() const; // 窗口的理性尺寸
- protected:
- void mousePressEvent(QMouseEvent *event);
- void mouseMoveEvent(QMouseEvent *event);
- void paintEvent(QPaintEvent *event); // 必須
- private:
- QImage image;
- int zoom;
- };
- #endif
- IconEditor::IconEditor(QWidget *parent)
- : QWidget(parent)
- {
- setAttribute(Qt::WA_StaticContents);
- // 告訴佈局管理器,理想尺寸是它的最小尺寸,不能對它縮小!
- setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
- zoom = 8; // 一個像素將顯示成一個8x8的正方形
- image = QImage(16, 16, QImage::Format_ARGB32); // 16x16像素的圖片
- image.fill(qRgba(0, 0, 0, 0)); // 黑色,完全透明
- }
- void IconEditor::sizeHint() const
- {
- QSize size = zoom * image.size();
- if (zoom >=3)
- size += QSize(1, 1);
- return size;
- }
- void IconEditor::setIconImage(const QImage &img)
- {
- if (img != image)
- {
- image = img.convertToFormat(QImage::Format_ARGB32);
- update(); // 重繪窗口
- updateGeometry(); // 告訴佈局管理器,理想尺寸已經發生改變,佈局需要調整
- }
- }
自動調用的情況:
- 窗口第一次顯示
- 大小改變
- 被遮擋,然後再次顯示
主動調用的情況:
update();
repaint(); // 不常用
- void IconEditor::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- painter.setPen(palette().forground().color()); // 調色板
- painter.drawLine(...);
- }
- void IconEditor::mousePressEvent(QMouseEvent *event)
- {
- if (event->button() == Qt::LeftButton)
- {
- setImagePixel(event->pos(), true);
- ...
- }
- else if (event->button() == Qt::LeftButton)
- {
- setImagePixel(event->pos(), false);
- ...
- }
- }
窗口屬性Qt::WA_StaticContents
當重新改變窗口部件的大小時,窗口部件的內容並沒有發生改變,
而且內容仍舊保留從窗口左上角開始的特性。這樣就可以避免重繪
已經顯示的區域。
在設計師中集成自定義窗口部件的2種方法
- 提升法:拖動一個自定義窗口的父窗口對象,右鍵->提示爲...
- 插件法:創建一個插件庫
當應用程序的最後一個窗口關閉時,程序退出
在main中使用下面語句:
QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
鍵盤:
Qt::Key_Plus: 對數字小鍵盤起作用,對於大鍵盤,要同時按下Shift
Qt::Key_Enter: 對數字小鍵盤起作用
當定義一個函數時,如果沒有用到其中的參數p,但又不想在編譯時產生警告:
在函數的開頭,使用宏
Q_UNUSED(p);
在QGraphicsItem的paint函數中,如果不希望線的寬度縮放,則
painter->setPen(color); //不指定pen的寬度,或者指定爲0.