C++ GUI Qt4编程(12)-6.1FindFileDialog

1. 主要介绍了QGridLayout, QHBoxLayout, QVBoxLayout3种布局管理器的使用方法。

2. 在linux中,继承自QDialog的对话框,没有最大化、最小化、关闭按钮,如果需要这3个按钮,

    需要增加:  setWindowFlags(Qt::Widget);

    在windows中,即使不加:setWindowFlags(Qt::Widget); 也会有最大化、最小化、关闭按钮。

3. findfiledialog.h

 1 /**/
 2 #ifndef FINDFILEDIALOG_H
 3 #define FINDFILEDIALOG_H
 4 
 5 #include <QDialog>
 6 
 7 class QLabel;
 8 class QLineEdit;
 9 class QPushButton;
10 class QCheckBox;
11 class QTableWidget;
12 class QHBoxLayout;
13 class QVBoxLayout;
14 class QGridLayout;
15 
16 class FindFileDialog : public QDialog
17 {
18     Q_OBJECT
19     
20 public:
21     FindFileDialog(QWidget *parent = 0);
22     
23 private:
24     QLabel *namedLabel;
25     QLabel *lookInLabel;
26     QLineEdit *namedLineEdit;
27     QLineEdit *lookInLineEdit;
28     QCheckBox *subfoldersCheckBox;
29     QTableWidget *tableWidget;
30     QLabel *messageLabel;
31     
32     QPushButton *findButton;
33     QPushButton *stopButton;
34     QPushButton *closeButton;
35     QPushButton *helpButton;
36     
37     QGridLayout *leftLayout;
38     QVBoxLayout *rightLayout;
39     QHBoxLayout *mainLayout;    
40 };
41 
42 
43 #endif

4. findfiledialog.cpp

 1 /**/
 2 #include "findfiledialog.h"
 3 
 4 #include <QLabel>
 5 #include <QLineEdit>
 6 #include <QPushButton>
 7 #include <QCheckBox>
 8 #include <QTableWidget>
 9 #include <QHBoxLayout>
10 #include <QVBoxLayout>
11 #include <QGridLayout>
12 
13 FindFileDialog::FindFileDialog(QWidget *parent)
14     : QDialog(parent)
15 {
16     namedLabel = new QLabel(tr("&Named:"));
17     namedLineEdit = new QLineEdit;
18     namedLabel->setBuddy(namedLineEdit);
19     
20     lookInLabel = new QLabel(tr("&Look in:"));
21     lookInLineEdit = new QLineEdit;
22     lookInLabel->setBuddy(lookInLineEdit);
23     
24     subfoldersCheckBox = new QCheckBox(tr("Include subfolders"));
25     
26     QStringList labels;
27     labels << tr("Name") << tr("In Folder")
28            << tr("Size") << tr("Modified");
29 
30     tableWidget = new QTableWidget;
31     tableWidget->setColumnCount(4);
32     tableWidget->setHorizontalHeaderLabels(labels);
33     
34     messageLabel = new QLabel(tr("0 files found"));
35     messageLabel->setFrameShape(QFrame::Panel);
36     messageLabel->setFrameShadow(QFrame::Sunken);
37     
38     /*  QGridLayout
39     void    addWidget(QWidget * widget, 
40                       int fromRow,int fromColumn, 
41                       int rowSpan,int columnSpan, 
42                       Qt::Alignment alignment = 0)
43     */
44     leftLayout = new QGridLayout;
45     leftLayout->addWidget(namedLabel, 0, 0);
46     leftLayout->addWidget(namedLineEdit, 0, 1);
47     leftLayout->addWidget(lookInLabel, 1, 0);
48     leftLayout->addWidget(lookInLineEdit, 1, 1);
49     /*subfoldersCheckBox的位置坐标(2, 0),占1行,占2列*/
50     leftLayout->addWidget(subfoldersCheckBox, 2, 0, 1, 2);
51     leftLayout->addWidget(tableWidget, 3, 0, 1, 2);
52     leftLayout->addWidget(messageLabel, 4, 0, 1, 2);
53     
54     
55     findButton = new QPushButton(tr("&Find"));
56     stopButton = new QPushButton(tr("&Stop"));
57     closeButton = new QPushButton(tr("&Close"));
58     helpButton = new QPushButton(tr("&Help"));
59     
60     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
61     
62     rightLayout = new QVBoxLayout;
63     rightLayout->addWidget(findButton);
64     rightLayout->addWidget(stopButton);
65     rightLayout->addWidget(closeButton);
66     rightLayout->addStretch();
67     rightLayout->addWidget(helpButton);
68     
69     mainLayout = new QHBoxLayout;
70     mainLayout->addLayout(leftLayout);
71     mainLayout->addLayout(rightLayout);
72     
73     setLayout(mainLayout);
74     setWindowTitle(tr("Find Files or Folders"));
75     /*在linux中,继承自QDialog的对话框没有最大化最小化和关闭按钮*/
76     setWindowFlags(Qt::Widget);     /*在linux中让对话框有最大最小和关闭按钮*/
77 }

5. main.cpp

 1 /**/
 2 #include <QApplication>
 3 #include "findfiledialog.h"
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication app(argc, argv);
 8     
 9     FindFileDialog dialog;
10     dialog.show();
11     
12     return app.exec();
13 }

6. 不带关闭按钮:

带关闭按钮:

posted @ 2017-08-15 22:09  seifguo  阅读(295)  评论(0编辑  收藏  举报