QT标准对话框


初学QT,完全没有一点概念,在一本书上面看了几遍代码,刚刚对QT有一个简单的认识,与大家分享一下书上面的代码  
  1. //CommonDialog.h   
  2. #ifndef COMMONDIALOG_H   
  3. #define COMMONDIALOG_H   
  4. #include <QDialog>   
  5. class QPushButton;  
  6. class QLineEdit;  
  7. class QFrame;  
  8. class QGridLayout;  
  9. class StandardDialogs:public QDialog  
  10. {  
  11.   Q_OBJECT  
  12.   
  13. public:  
  14.   StandardDialogs(QWidget *parent = 0, Qt::WindowFlags f = 0);  
  15.   ~StandardDialogs();  
  16.   
  17. public:  
  18.   QGridLayout *layout;  
  19.   QPushButton *filePushButton;  
  20.   QPushButton *colorPushButton;  
  21.   QPushButton *fontPushButton;  
  22.   QLineEdit *fileLineEdit;  
  23.   QLineEdit *fontLineEdit;  
  24.   QFrame *colorFrame;  
  25.   
  26. private slots:  
  27.   void slotOpenFileDlg();  
  28.   void slotOpenColorDlg();  
  29.   void slotOpenFontDlg();  
  30. };  
  31. #endif // COMMONDIALOG_H  

 

 
  1. //CommonDialog.cpp   
  2. #include "CommonDialog.h"   
  3. #include <QtGui>   
  4. StandardDialogs::StandardDialogs(QWidget *parent, Qt::WindowFlags f):QDialog(parent, f)  
  5. {  
  6.   setWindowTitle(tr("Standard Dialogs"));  
  7.   
  8.   layout = new QGridLayout(this);  
  9.   
  10.   filePushButton = new QPushButton;  
  11.   filePushButton->setText(tr("File Dialog"));  
  12.   
  13.   colorPushButton = new QPushButton;  
  14.   colorPushButton->setText(tr("Color Dialog"));  
  15.   
  16.   fontPushButton = new QPushButton;  
  17.   fontPushButton->setText(tr("Font Dialog"));  
  18.   
  19.   fileLineEdit = new QLineEdit;  
  20.   
  21.   colorFrame = new QFrame;  
  22.   colorFrame->setFrameShape(QFrame::Box);  
  23.   colorFrame->setAutoFillBackground(true);  
  24.   
  25.   fontLineEdit = new QLineEdit;  
  26.   fontLineEdit->setText(tr("Hello World"));  
  27.   
  28.   layout->addWidget(filePushButton, 0, 0);  
  29.   layout->addWidget(fileLineEdit, 0, 1);  
  30.   layout->addWidget(colorPushButton, 1, 0);  
  31.   layout->addWidget(colorFrame, 1, 1);  
  32.   layout->addWidget(fontPushButton, 2, 0);  
  33.   layout->addWidget(fontLineEdit, 2, 1);  
  34.   layout->setMargin(15);  
  35.   layout->setSpacing(10);  
  36.   
  37.   connect(filePushButton, SIGNAL(clicked()), this, SLOT(slotOpenFileDlg()));  
  38.   connect(colorPushButton, SIGNAL(clicked()), this, SLOT(slotOpenColorDlg()));  
  39.   connect(fontPushButton, SIGNAL(clicked()), this, SLOT(slotOpenFontDlg()));  
  40. }  
  41.   
  42. StandardDialogs::~StandardDialogs()  
  43. {  
  44. }  
  45.   
  46. void StandardDialogs::slotOpenFileDlg()  
  47. {  
  48.   QString s = QFileDialog::getOpenFileName(  
  49.                   this,  
  50.                   "open file dialog",  
  51.                   "/",  
  52.                   "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");  
  53.   fileLineEdit->setText(s.toAscii());  
  54. }  
  55.   
  56. void StandardDialogs::slotOpenColorDlg()  
  57. {  
  58.   QColor color = QColorDialog::getColor(Qt::blue);  
  59.   if(color.isValid())  
  60.   {  
  61.       colorFrame->setPalette(QPalette(color));  
  62.   }  
  63. }  
  64.   
  65. void StandardDialogs::slotOpenFontDlg()  
  66. {  
  67.   bool ok;  
  68.   QFont font = QFontDialog::getFont(&ok);  
  69.   if(ok)  
  70.   {  
  71.       fontLineEdit->setFont(font);  
  72.   }  
  73. }   
 
  1. //main.cpp   
  2.   
  3. #include <QApplication>   
  4.   
  5. #include "CommonDialog.h"   
  6.   
  7. int main(int argc, char *argv[])  
  8.   
  9. {  
  10.   
  11.   QApplication app(argc, argv);  
  12.   
  13.   StandardDialogs *sd = new StandardDialogs;  
  14.   
  15.   sd->show();  
  16.   
  17.   return app.exec();  
  18.   
  19. }  

程序运行截图:

相关内容