#include " mainwindow.h " #include <QMenuBar> #include <QMenu> #include <QAction> #include <QDialog> #include <QDebug> #include <QMessageBox> #include <QFileDialog> MainWindow :: MainWindow (QWidget * parent ) : The QMainWindow (parent) { QMenuBar * = mBar the menuBar (); setMenuBar (mBar); The QMenu * = mBar- MENU> AddMenu ( " box " ); // use dialog // About box * P3 = menu- QAction> addAction ( " About Box " ); Connect (p3, & QAction :: triggered, [= ] () { / * After viewing QmessageBox help documentation, which the Static Public Members, to see inside about use function * static Public members learned from its static member function, here it comes to static member function call inside the class by class name that is invoked;. * the last call as follows: * :: QMessageBox the About ( this, "about", "on QT"); * * / a QMessageBox :: About ( the this , " About " , " on QT " ); }); // problem dialog The QAction * P4 = Menu-> the addAction ( "Question dialog "); Connect (P4, & QAction :: triggered, [= ] () { / * process of creating problems here and dialog on a similar, but there is a need to pay attention, there are two buttons on top of the dialog box questions, * how know what I clicked on a button? * QMessageBox by looking at the help documentation, StandardButton contents inside Public Types inside, you can see different buttons enumeration of * that it will return a value, so we define a value for receiving as follows: ( "? the ARE YOU the OK" the this, "Question",) * = int RET QMessageBox :: Question; * * of course, sometimes we do not want only two buttons YES and NO, there is also can be changed change the code is as follows: * int = RET QMessageBox :: Question (the this, "Question", "the OK the aRE YOU?", the OK QMessageBox :: | :: QMessageBox the Cancel); * this time inside the button turns OK button and a cancel button. * / int RET = QMessageBox :: Question ( the this , "question","ARE YOU OK ?"); switch(ret) { case QMessageBox::Yes: qDebug()<<"I am OK "; break; case QMessageBox::No: qDebug()<<"NO OK"; break; default: break; } }); //文件对话框 QAction *p5 = menu->addAction(" File Dialog " ); Connect (P5, & The QAction :: triggered, [= ] () { / * open a file, the display content, display path, * getOpenFileName By examining the contents inside, (which in fact has been written examples) * QFileDialog :: getOpenFileName (the this, "Open", "../"); have a return value, * = QFileDialog :: getOpenFileName QString path (the this, "Open", "../"); * ? If you want to open the specified file format how to write * Note: when you want to open a variety of file types, using two ;; to represent * * / QString path = QFileDialog :: getOpenFileName ( the this , " open " , " ../ " , " Source (. * cpp * .h);;Text(*.txt);;all(*.*)"); qDebug()<<path; }); } MainWindow::~MainWindow() { }
getOpenFileName ( the this, " Open ", " ../ ", " Source (CPP * * .h) ;; the Text (* TXT) ;; All (* *)... " ); the " Source (*. * .h cpp) ;; Text (*. TXT) ;; All (*. *) " a very long time, you can ;; press enter after wrap, this operation in QT there is no problem.
MAINWINDOW_H #ifndef #define MAINWINDOW_H #include <The QMainWindow> #include <QDialog> class the MainWindow: public The QMainWindow { the Q_OBJECT public : the MainWindow (the QWidget * parent = 0 ); ~ the MainWindow (); QDialog DLG; // custom global variables ( member variables) }; #endif // MAINWINDOW_H