#include <qlineedit.h> QLineEdit::QlineEdit (QWidget *parent, const char* name = 0 ) QLineEdit::QLineEdit (const QString &contents, QWidget *parent, const char *name = 0 ) QLineEdit::QLineEdit (const QString &contents, const QString &inputMask, QWidget *parent, const char *name = 0 ) void setInputMask (const QString &inputMask) void insert (const QString &newText ) bool isModified (void) void setMaxLength (int length) void setReadOnly (bool read) void setText (const QString &text) QString text (void) void setEchoMode(EchoMode mode)
lineEdit->setEchoMode(QLineEdit::Password);
意義 | 必要性字元 | 選擇性字元 |
ASCII A-Z,a-z | A | a |
ASCII A-Z,a-z,0-9 | N | n |
任何字元 | X | x |
數值 0-9 | 9 | 0 |
數值 1-9 | D | d |
# |
數字或 +, - 符號之選擇性字元。 |
> |
將隨後的字元變成大寫。 |
< |
將隨後的字元變成小寫。 |
! |
停止轉換。 |
\ |
跳脫字元 |
#include <qmainwindow.h> #include <qlineedit.h> #include <qstring.h> class LineEdit : public QMainWindow { Q_OBJECT public: LineEdit(QWidget *parent = 0, const char *name = 0); QLineEdit *password_entry; private slots: void Clicked(); };
#include "LineEdit.moc" #include <qpushbutton.h> #include <qapplication.h> #include <qlabel.h> #include <qlayout.h> #include <iostream> LineEntry::LineEntry(QWidget *parent, const char *name) : QMainWindow(parent, name) { QWidget *widget = new QWidget(this); setCentralWidget(widget); # 使用 QGridLayout 來安排 widget。指定行數、列數、邊界設定和間隔。 QGridLayout *grid = new QGridLayout(widget,3,2,10, 10,"grid"); QLineEdit *username_entry = new QLineEdit( widget, "username_entry"); password_entry = new QLineEdit( widget, "password_entry"); password_entry->setEchoMode(QLineEdit::Password); # 在方格中加入一個 widget,必須告知行編號、列編號,而起始值為(0,0)代表左上方的格子。 grid->addWidget(new QLabel("Username", widget, "userlabel"), 0, 0, 0); grid->addWidget(new QLabel("Password", widget, "passwordlabel"), 1, 0, 0); grid->addWidget(username_entry, 0,1, 0); grid->addWidget(password_entry, 1,1, 0); QPushButton *button = new QPushButton ("Ok", widget, "button"); grid->addWidget(button, 2,1,Qt::AlignRight); resize( 350, 200 ); connect (button, SIGNAL(clicked()), this, SLOT(Clicked())); } void LineEntry::Clicked(void) { std::cout << password_entry->text() << "\n"; } int main(int argc, char **argv) { QApplication app(argc,argv); LineEntry *window = new LineEntry(); app.setMainWidget(window); window->show(); return app.exec(); }
[dywang@dywOffice chapter17]$ moc LineEdit.h -o LineEdit.moc [dywang@dywOffice chapter17]$ g++ -o lineedit LineEdit.cpp \ -I$QTDIR/include -L$QTDIR/lib -lqt-mt [dywang@dywOffice chapter17]$ ./lineedit
#include <qbutton.h> virtual void QButton::setText ( const QString & ) virtual void QButton::setPixmap ( const QPixmap & ) bool QButton::isToggleButton () const virtual void QButton::setDown ( bool ) bool QButton::isDown () const bool QButton::isOn () const enum QButton::ToggleState { Off, NoChange, On } ToggleState QButton::state () const
#include <qpushbutton.h> QPushButton (QWidget *parent, const char *name = 0) QPushButton (const QString &text, QWidget *parent, const char *name = 0) QPushButton (const QIconSet &icon, const QString &text, QWidget *parent, const char * name = 0 ) void QPushButton::setToggleButton (bool);
#include <qcheckbox.h> QCheckBox (QWidget *parent, const char *name = 0 ) QCheckBox (const QString &text, QWidget *parent, const char *name = 0 ) bool QCheckBox::isChecked () void QCheckBox::setTristate ( bool y = TRUE ) bool QCheckBox::isTristate ()
#include <qradiobutton.h> QRadioButton (QWidget *parent, const char *name = 0 ) QRadioButton (const QString &text, QWidget *parent, const char *name = 0 ) bool QRadioButton::isChecked ()
#include <qbuttongroup.h> QButtonGroup (QWidget *parent = 0, const char * name = 0 ) QButtonGroup (const QString & title, QWidget * parent = 0, const char * name = 0 ) int insert (QButton *button, int id = -1) void remove (QButton *button) int id (QButton *button) const int count () const int selectedId () const
#include <qmainwindow.h> #include <qcheckbox.h> #include <qbutton.h> #include <qradiobutton.h> class Buttons : public QMainWindow { Q_OBJECT public: Buttons(QWidget *parent = 0, const char *name = 0); # 在 slot 函式中會查詢按鈕的狀態,所以在類別定義中宣告按鈕指標為私有的。 private: void PrintActive(QButton *button); QCheckBox *checkbox; QRadioButton *radiobutton1, *radiobutton2; private slots: void Clicked(); };
#include "Buttons.moc" #include <qbuttongroup.h> #include <qpushbutton.h> #include <qapplication.h> #include <qlabel.h> #include <qlayout.h> #include <iostream> Buttons::Buttons(QWidget *parent, const char *name) : QMainWindow(parent, name) { QWidget *widget = new QWidget(this); setCentralWidget(widget); QVBoxLayout *vbox = new QVBoxLayout(widget,5, 10,"vbox"); checkbox = new QCheckBox("CheckButton", widget, "check"); vbox->addWidget(checkbox); # 為兩個 radio 按鈕,產生一個 QButtonGroup: QButtonGroup *buttongroup = new QButtonGroup(0); radiobutton1 = new QRadioButton("RadioButton1", widget, "radio1"); buttongroup->insert(radiobutton1); vbox->addWidget(radiobutton1); radiobutton2 = new QRadioButton("RadioButton2", widget, "radio2"); buttongroup->insert(radiobutton2); vbox->addWidget(radiobutton2); QPushButton *button = new QPushButton ("Ok", widget, "button"); vbox->addWidget(button); resize( 350, 200 ); connect (button, SIGNAL(clicked()), this, SLOT(Clicked())); }
void Buttons::PrintActive(QButton *button) { if (button->isOn()) std::cout << button->name() << " is checked\n"; else std::cout << button->name() << " is not checked\n"; } void Buttons::Clicked(void) { PrintActive(checkbox); PrintActive(radiobutton1); PrintActive(radiobutton2); std::cout << "\n"; } int main(int argc, char **argv) { QApplication app(argc,argv); Buttons *window = new Buttons(); app.setMainWidget(window); window->show(); return app.exec(); }
[dywang@dywOffice chapter17]$ moc Buttons.h -o Buttons.moc [dywang@dywOffice chapter17]$ g++ -o buttons Buttons.cpp\ >-I$QTDIR/include -L$QTDIR/lib -lqt-mt [dywang@dywOffice chapter17]$ ./buttons check is checked radio1 is checked radio2 is not checked
# QComboBox 建構函式中的布林參數 TRUE,指定 QComboBox 為可讀寫。 QComboBox *combo = new QComboBox(TRUE, parent, “widgetname”);
combo->insertItem(QString(“An Item”), 1); # 數值 1 代表設定這個選項為串列中的第一個選項。 # 如果要將加到串列尾端,只要傳入任何負的整數。
char* weather[] = {“Thunder”, “Lightning”, “Rain”, 0}; combo->insertStrList(weather, 3);
combo->setInsertionPolicy(QComboBox::AtTop);
關鍵字串 | 插入策略 |
QComboBox::AtTop | 將新項目插到串列的第一個選項。 |
QComboBox::AtBottom | 將新項目插到串列的最後一個選項。 |
QComboBox::AtCurrent | 取代先前選擇的選項。 |
QComboBox::BeforeCurrent | 將新項目插到先前選擇的選項之前。 |
QComboBox::AfterCurrent | 將新項目插到先前選擇的選項之後。 |
QComboBox::NoInsertion | 不要將新選項插入串列中。 |
#include <qcombobox.h> QComboBox (QWidget *parent = 0, const char *name = 0) QComboBox (bool readwrite, QWidget *parent = 0, const char *name = 0) # count 回覆串列選項的數量。 int count () # QStringList 和 QStrList 是 Qt 字串集合類別,可以用來加入多個選項。 void insertStringList (const QStringList &list, int index = -1) void insertStrList (const QStrList &list, int index = -1) void insertStrList (const QStrList *list, int index = -1) void insertStrList (const char **strings, int numStrings = -1, int index = -1) # insertItem 加入選項; void insertItem (const QString &t, int index = -1) # removeItem() 移除選項; void removeItem (int index) # CurrrentItem() 取得目前選項; virtual void setCurrentItem (int index) # CurrrentText() 取得目前選項內容; QString currentText () virtual void setCurrentText (const QString &) # setEditable() 切換可編輯的狀態。 void setEditable (bool)
#include "ComboBox.moc" #include <qlayout.h> #include <iostream> ComboBox::ComboBox(QWidget *parent, const char *name) : QMainWindow(parent, name) { QWidget *widget = new QWidget(this); setCentralWidget(widget); QVBoxLayout *vbox = new QVBoxLayout(widget, 5, 10,"vbox"); QComboBox *editablecombo = new QComboBox(TRUE, widget, "editable"); vbox->addWidget(editablecombo); QComboBox *readonlycombo = new QComboBox(FALSE, widget, "readonly"); vbox->addWidget(readonlycombo); static const char* items[] = { "Macbeth", "Twelfth Night", "Othello", 0 }; editablecombo->insertStrList (items); readonlycombo->insertStrList (items); # 當一個新選項被選擇之後,QComboBox 會發出 textChanged(QString &) 信號。 connect (editablecombo, SIGNAL(textChanged(const QString&)), this, SLOT(Changed(const QString&))); resize( 350, 200 ); } # QString 參數 s,會被信號傳送。 void ComboBox::Changed(const QString& s) { std::cout << s << "\n"; } int main(int argc, char **argv) { QApplication app(argc,argv); ComboBox *window = new ComboBox(); app.setMainWidget(window); window->show(); return app.exec(); }
[dywang@dywOffice chapter17]$ moc ComboBox.h -o ComboBox.moc [dywang@dywOffice chapter17]$ g++ -o combobox ComboBox.cpp \ >-I$QTDIR/include -L$QTDIR/lib -lqt-mt [dywang@dywOffice chapter17]$ ./combobox Twelfth Night
QListView *view = new QListView(parent, “name”);
view->addColumn("Left Column", width1 ); // Fixed width view->addColumn("Right Column"); // Width autosizes
QListViewItem::QListViewItem ( QListView * parent, QString label1, QString label2 = QString::null, QString label3 = QString::null, QString label4 = QString::null, QString label5 = QString::null, QString label6 = QString::null, QString label7 = QString::null, QString label8 = QString::null ) # 可以提供八個行的標籤,若不需要可不給參數或指定 NULL
QListViewItem *toplevel = new QListViewItem(view, "Left Data", "Right Data");
new QListViewItem(toplevel, "Left Data", "Right Data"); // A Child of toplevel
#include <qlistview.h> virtual void insertItem ( QListViewItem * newChild ) virtual void setText ( int column, const QString & text ) virtual QString text ( int column ) const QListViewItem *firstChild () const QListViewItem *nextSibling () const QListViewItem *parent () const QListViewItem *itemAbove () QListViewItem *itemBelow ()
QListViewItem *child = view->firstChild(); while(child) { cout << myChild->text(1) << "\n"; myChild = myChild->nextSibling(); }
#include "ListView.moc" ListView::ListView(QWidget *parent, const char *name) : QMainWindow(parent, name) { listview = new QListView(this, "listview1"); listview->addColumn("Artist"); listview->addColumn("Title"); listview->addColumn("Catalogue"); listview->setRootIsDecorated(TRUE); QListViewItem *toplevel = new QListViewItem(listview, "Avril Lavigne", "Let Go", "AVCD01"); new QListViewItem(toplevel, "Complicated"); new QListViewItem(toplevel, "Sk8er Boi"); setCentralWidget(listview); } int main(int argc, char **argv) { QApplication app(argc,argv); ListView *window = new ListView(); app.setMainWidget(window); window->show(); return app.exec(); }
[dywang@dywOffice chapter17]$ moc ListView.h -o ListView.moc [dywang@dywOffice chapter17]$ g++ -o listview ListView.cpp \ -I$QTDIR/include -L$QTDIR/lib -lqt-mt [dywang@dywOffice chapter17]$ ./listview