QT學習 之 對話方塊 (一) 可擴充套件對話方塊
QT中的對話方塊是QDialog類
下面是三個主要的視窗的區別:
QWidget類是所有使用者介面物件的基類。 視窗部件是使用者介面的一個基本單元:它從視窗系統接收滑鼠、鍵盤和其它事件,並且在螢幕上繪製自己。每一個視窗部件都是矩形的,並且它們按Z軸順序排列。一個視窗部件可以被它的父視窗部件或者它前面的視窗部件蓋住一部分。
QMainWindow 類提供一個有選單條、錨接視窗(例如工具條)和一個狀態條的主應用程式視窗。主視窗通常用在提供一個大的中央視窗部件(例如文字編輯或者繪製畫布)以及周圍 選單、工具條和一個狀態條。QMainWindow常常被繼承,因為這使得封裝中央部件、選單和工具條以及視窗狀態條變得更容易,當用戶點選選單項或者工具條按鈕時,槽會被呼叫。
QDialog類是對話方塊視窗的基類。對話方塊視窗是主要用於短期任務以及和使用者進行簡要通訊的頂級視窗。QDialog可以是模態對話方塊也可以是非模態對話方塊。QDialog支援擴充套件性並且可以提供返回值。它們可以有預設按鈕。QDialog也可以有一個QSizeGrip在它的右下角,使用setSizeGripEnabled()。
QDialog 是最普通的頂級視窗。一個不會被嵌入到父視窗部件的視窗部件叫做頂級視窗部件。通常情況下,頂級視窗部件是有框架和標題欄的視窗(儘管使用了一定的視窗部件標記,建立頂級視窗部件時也可能沒有這些裝飾。)在Qt中,QMainWindow和不同的QDialog的子類是最普通的頂級視窗。
如果是頂級對話方塊,那就基於QDialog建立,如果是主窗體,那就基於QMainWindow,如果不確定,或者有可能作為頂級窗體,或有可能嵌入到其他窗體中,則基於QWidget建立。
當然了,實際中,你還可以基於任何其他部件類來派生。看實際需求了,比如QFrame、QStackedWidget等等。
extension.h標頭檔案
<span style="font-size:18px;">#ifndef EXTENSION_H #define EXTENSION_H #include <QApplication> #include <QPushButton> #include <QDialog> #include <QGridLayout> #include <QWidget> #include <QHBoxLayout> #include <QLabel> #include <QLineEdit> #include <QComboBox> #include <QDialogButtonBox> #include <QGridLayout> #include <QDialog> #include <QtGui> class Extension : public QDialog { Q_OBJECT public: Extension(QWidget *parent=0); void createBaseInfo(); void createDetailInfo(); public slots: void slotExtension(); private: QWidget *baseWidget; QWidget *detailWidget; }; #endif // EXTENSION_H </span>
extension.cpp
QComboBOX:下拉列表框
常用方法和屬性:
(1)addItems
void addItems ( const QStringList & texts )在QComboBox的最後新增一項。
(2)count
int count () const返回列表項總數。
(3)currentIndex
int currentIndex () const當前顯示的列表項序號。
(4)currentText
QString currentText () const返回當前顯示的文字。
(5)insertItem
void insertItem ( int index, const QString & text, const QVariant & userData = QVariant() )
void insertItem ( int index, const QIcon & icon, const QString & text, const QVariant & userData = QVariant() )
void insertItems ( int index, const QStringList & list )
插入一項或多項至序號index處。
(6)insertSeparator
在序號為index的項前插入分隔線
(7)setItemText
void setItemText ( int index, const QString & text )改變序號為index項的文字。
QDialogButtonBox:用於建立一個符合當前視窗部件樣式的一組按鈕,並且它們被排列在某種佈局之中。
在QtDesigner中,最為常見的用法是從視窗部件盒裡面把預設的那個QDialogButtonBox視窗部件拖到介面上來,不過顯然這並不如使用程式碼來得方便。
<span style="font-size:18px;">#include "extension.h"
Extension::Extension(QWidget *parent):QDialog(parent)
{
setWindowTitle(tr("Extension Dialog"));
createBaseInfo();
createDetailInfo();
QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(baseWidget);
layout->addWidget(detailWidget);
layout->setSizeConstraint(QLayout::SetFixedSize);
layout->setSpacing(10);
setLayout(layout);
}
void Extension::createBaseInfo()
{
baseWidget=new QWidget;
QLabel *nameLabel = new QLabel(tr("Name:"));
QLineEdit *nameEdit = new QLineEdit;
QLabel *sexLabel = new QLabel(tr("Sex:"));
QComboBox *sexComboBox = new QComboBox;
sexComboBox->addItem("male");
sexComboBox->addItem("female");
QPushButton *okButton = new QPushButton(tr("OK"));
QPushButton *detailButton = new QPushButton(tr("Detail"));
connect(detailButton,SIGNAL(clicked()),this,SLOT(slotExtension()));
QDialogButtonBox *btnBox = new QDialogButtonBox(Qt::Vertical);
btnBox->addButton(okButton,QDialogButtonBox::ActionRole);
btnBox->addButton(detailButton,QDialogButtonBox::ActionRole);
QGridLayout *gride = new QGridLayout;
gride->addWidget(nameLabel,0,0);
gride->addWidget(sexLabel,1,0);
gride->addWidget(nameEdit,0,1);
gride->addWidget(sexComboBox,1,1);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addLayout(gride);
hbox->addStretch();
hbox->addWidget(btnBox);
baseWidget->setLayout(hbox);
}
void Extension::createDetailInfo()
{
detailWidget = new QWidget;
QLabel *label1 = new QLabel(tr("Age"));
QLineEdit *ageEdit = new QLineEdit;
ageEdit->setText("30");
QLabel *label2 = new QLabel(tr("Department"));
QComboBox *deptComboBox = new QComboBox;
deptComboBox->addItem(tr("dept 1"));
deptComboBox->addItem(tr("dept 2"));
deptComboBox->addItem(tr("dept 3"));
deptComboBox->addItem(tr("dept 4"));
QLabel *label3 = new QLabel(tr("email:"));
QLineEdit *edit = new QLineEdit;
QGridLayout *grid = new QGridLayout;
grid->addWidget(label1,0,0);
grid->addWidget(ageEdit,0,1);
grid->addWidget(label2,1,0);
grid->addWidget(deptComboBox,1,1);
grid->addWidget(label3,2,0);
grid->addWidget(edit,2,1);
detailWidget->setLayout(grid);
detailWidget->hide();
}
void Extension::slotExtension()
{
if(detailWidget->isHidden())
{
detailWidget->show();
}
else
{
detailWidget->hide();
}
}
</span>
main.cpp
<span style="font-size:18px;">#include<QApplication>
#include"extension.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
Extension exten;
exten.show();
return app.exec();
}
</span>