在已有類mainwindow上,新建對話框。
設計模式
首先在頭文件加前置聲明,實現文件上添加包含的頭文件。編輯器
findDlg = new QDialog(this); //指針,新建要加的對話框findDlg findDlg->setWindowTitle(tr("查找"));//設置標題 findLineEdit = new QLineEdit(findDlg);//findDlg上添加一個QLineEdit QPushButton *btn= new QPushButton(tr("查找下一個"), findDlg);//findDlg上添加一個按鈕 QVBoxLayout *layout= new QVBoxLayout(findDlg);//findDlg上設置佈局 layout->addWidget(findLineEdit); layout->addWidget(btn);//佈局上添加組件 connect(btn, SIGNAL(clicked()), this, SLOT(showFindText()));//槽函數,下面實現這個函數
彈出一個QMessageBox函數
void MainWindow::showFindText() { QString str=findLineEdit->text(); if (!ui->textEdit->find(str, QTextDocument::FindBackward)) { QMessageBox::warning(this, tr("查找"), tr("找不到%1").arg(str)); } }
點擊查找彈出查找對話框佈局
void MainWindow::on_action_Find_triggered() { findDlg->show();//顯示一個查找對話框 }
添加Action動做狀態提示(窗口的最左下角的提示),在屬性的statusTipui
響應鼠標左鍵和鍵盤消息this
//1.在widget.h文件添加鼠標按下事件處理函數聲明: protected: void mousePressEvent(QMouseEvent *); //2.到widget.cpp文件中先添加頭文件包含: #include <QMouseEvent> //3.而後在下面添加函數的定義: void Widget::mousePressEvent(QMouseEvent *e) { ui->pushButton->setText(tr("(%1,%2)").arg(e->x()).arg(e->y())); }
//1.首先在widget.h中添加protected函數聲明: void keyPressEvent(QKeyEvent *); //2.而後到widget.cpp中添加頭文件包含: #include <QKeyEvent> //3.最後添加鍵盤按下事件處理函數的定義: void Widget::keyPressEvent(QKeyEvent *e) { int x = ui->pushButton->x(); int y = ui->pushButton->y(); switch (e->key()) { case Qt::Key_W : ui->pushButton->move(x, y-10); break; case Qt::Key_S : ui->pushButton->move(x, y+10); break; case Qt::Key_A : ui->pushButton->move(x-10, y); break; case Qt::Key_D : ui->pushButton->move(x+10, y); break; } }
定時器spa
//1.到widget.h文件中添加函數聲明: protected: void timerEvent(QTimerEvent *); 而後添加私有變量定義: int id1, id2, id3; //2.下面進入widget.cpp文件,先在構造函數中添加以下代碼: id1 = startTimer(1000); // 開啓一個1秒定時器,返回其ID id2 = startTimer(2000); id3 = startTimer(10000); //3.下面到設計模式,向界面上拖入兩個標籤部件Label。 //4.下面添加定時器事件處理函數的定義: void Widget::timerEvent(QTimerEvent *event) { if (event->timerId() == id1) { // 判斷是哪一個定時器 ui->label->setText(tr("%1").arg(qrand()%10)); } else if (event->timerId() == id2) { ui->label_2->setText(tr("hello world!")); } else { qApp->quit(); } } /*****************************************************/ /*若是隻是想開啓少許的定時器,也可使用信號和槽來實現。 先在widget.h中添加一個私有槽聲明:*/ private slots: void timerUpdate(); //而後到設計模式向界面上添加一個行編輯器部件Line Edit, //再到widget.cpp中添加頭文件包含: #include <QTimer> #include <QDateTime> //而後在構造函數中添加以下代碼: QTimer *timer = new QTimer(this); //關聯定時器溢出信號和相應的槽函數 connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate())); timer->start(1000); //這裏建立了一個定時器,並將其溢出信號和更新槽關聯起來,最後使用start()函數來開啓定時器。 //下面添加timerUpdate()函數的定義: void Widget::timerUpdate() { //獲取系統如今的時間 QDateTime time = QDateTime::currentDateTime(); //設置系統時間顯示格式 QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); //在標籤上顯示時間 ui->lineEdit->setText(str); }
隨機數設計
/* 關於隨機數,在Qt中是使用qrand()和qsrand()兩個函數實現的。 qrand()%10能夠產生0-9之間的隨機數。要想產生100之內的隨機數就是%100。以此類推。 在使用qrand()函數產生隨機數以前,通常要使用qsrand()函數爲其設置初值,若是不設置初值,那麼每次運行程序,qrand()都會產生相同的一組隨機數。 爲了每次運行程序時,均可以產生不一樣的隨機數,咱們要使用qsrand()設置一個不一樣的初值。 這裏使用了QTime類的secsTo()函數,它表示兩個時間點之間所包含的秒數,好比代碼中就是指從零點整到當前時間所通過的秒數。 */ //下面先在widget.cpp的構造函數中添加以下代碼: qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); //而後在timerUpdate()函數的最後添加以下代碼: int rand = qrand() % 300; // 產生300之內的正整數 ui->lineEdit->move(rand, rand); //這樣,每過一秒,行編輯器都會移動到一個隨機的位置。你們能夠運行程序,查看效果。