标签对话框示例
标签对话框示例展示了如何使用QTabWidget 类构建标签对话框。
对话框为应用程序与用户交流提供了一种有效的方式,但复杂的对话框往往会占用过多的屏幕面积。通过在对话框中使用多个标签,可以将信息分成不同的类别,同时保持信息的可访问性。
标签对话框示例由一个TabDialog
类组成,该类提供三个标签,每个标签包含一个特定文件的信息,还有两个标准按钮,用于接受或拒绝对话框的内容。
TabDialog 类定义
TabDialog
类是QDialog 的子类,显示QTabWidget 和两个标准对话框按钮。类的定义只包含类的构造函数和QTabWidget 的私有数据成员:
class TabDialog : public QDialog { Q_OBJECT public: explicit TabDialog(const QString &fileName, QWidget *parent = nullptr); private: QTabWidget *tabWidget; QDialogButtonBox *buttonBox; };
在示例中,窗口部件将作为顶层窗口使用,但我们定义了构造函数,使其可以接收父窗口部件。这样,对话框就可以居中位于应用程序主窗口的顶部。
TabDialog 类的实现
构造函数调用QDialog 构造函数,并为指定文件名创建QFileInfo 对象。
TabDialog::TabDialog(const QString &fileName, QWidget *parent) : QDialog(parent) { QFileInfo fileInfo(fileName); tabWidget = new QTabWidget; tabWidget->addTab(new GeneralTab(fileInfo), tr("General")); tabWidget->addTab(new PermissionsTab(fileInfo), tr("Permissions")); tabWidget->addTab(new ApplicationsTab(fileInfo), tr("Applications"));
标签窗口部件由三个自定义窗口部件填充,每个窗口部件都包含文件的相关信息。我们在构建这些 widget 时都没有父 widget,因为当这些 widget 添加到标签 widget 时,标签 widget 会将它们转为父 widget。
我们创建了两个标准按钮,并将它们分别连接到对话框中的相应插槽:
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
我们将标签窗口部件安排在对话框中按钮的上方:
QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(tabWidget); mainLayout->addWidget(buttonBox); setLayout(mainLayout);
最后,我们设置对话框的标题:
setWindowTitle(tr("Tab Dialog")); }
每个选项卡都是QWidget 的子类,只提供构造函数。
GeneralTab 类定义
GeneralTab widget 的定义很简单,因为我们只对在标签页中显示 widget 的内容感兴趣:
class GeneralTab : public QWidget { Q_OBJECT public: explicit GeneralTab(const QFileInfo &fileInfo, QWidget *parent = nullptr); };
GeneralTab 类的实现
GeneralTab widget 只需显示 TabDialog 传递的文件的一些信息。为此,我们使用了各种 widget,这些 widget 采用垂直布局:
GeneralTab::GeneralTab(const QFileInfo &fileInfo, QWidget *parent) : QWidget(parent) { QLabel *fileNameLabel = new QLabel(tr("File Name:")); QLineEdit *fileNameEdit = new QLineEdit(fileInfo.fileName()); QLabel *pathLabel = new QLabel(tr("Path:")); QLabel *pathValueLabel = new QLabel(fileInfo.absoluteFilePath()); pathValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); QLabel *sizeLabel = new QLabel(tr("Size:")); qlonglong size = fileInfo.size()/1024; QLabel *sizeValueLabel = new QLabel(tr("%1 K").arg(size)); sizeValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); QLabel *lastReadLabel = new QLabel(tr("Last Read:")); QLabel *lastReadValueLabel = new QLabel(fileInfo.lastRead().toString()); lastReadValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); QLabel *lastModLabel = new QLabel(tr("Last Modified:")); QLabel *lastModValueLabel = new QLabel(fileInfo.lastModified().toString()); lastModValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(fileNameLabel); mainLayout->addWidget(fileNameEdit); mainLayout->addWidget(pathLabel); mainLayout->addWidget(pathValueLabel); mainLayout->addWidget(sizeLabel); mainLayout->addWidget(sizeValueLabel); mainLayout->addWidget(lastReadLabel); mainLayout->addWidget(lastReadValueLabel); mainLayout->addWidget(lastModLabel); mainLayout->addWidget(lastModValueLabel); mainLayout->addStretch(1); setLayout(mainLayout); }
PermissionsTab 类定义
与 GeneralTab 类似,PermissionsTab 也只是用作其子控件的占位符:
class PermissionsTab : public QWidget { Q_OBJECT public: explicit PermissionsTab(const QFileInfo &fileInfo, QWidget *parent = nullptr); };
权限标签类实现
PermissionsTab 显示文件的访问信息,在嵌套布局的 widget 中显示文件权限和所有者的详细信息:
PermissionsTab::PermissionsTab(const QFileInfo &fileInfo, QWidget *parent) : QWidget(parent) { QGroupBox *permissionsGroup = new QGroupBox(tr("Permissions")); QCheckBox *readable = new QCheckBox(tr("Readable")); if (fileInfo.isReadable()) readable->setChecked(true); QCheckBox *writable = new QCheckBox(tr("Writable")); if ( fileInfo.isWritable() ) writable->setChecked(true); QCheckBox *executable = new QCheckBox(tr("Executable")); if ( fileInfo.isExecutable() ) executable->setChecked(true); QGroupBox *ownerGroup = new QGroupBox(tr("Ownership")); QLabel *ownerLabel = new QLabel(tr("Owner")); QLabel *ownerValueLabel = new QLabel(fileInfo.owner()); ownerValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); QLabel *groupLabel = new QLabel(tr("Group")); QLabel *groupValueLabel = new QLabel(fileInfo.group()); groupValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); QVBoxLayout *permissionsLayout = new QVBoxLayout; permissionsLayout->addWidget(readable); permissionsLayout->addWidget(writable); permissionsLayout->addWidget(executable); permissionsGroup->setLayout(permissionsLayout); QVBoxLayout *ownerLayout = new QVBoxLayout; ownerLayout->addWidget(ownerLabel); ownerLayout->addWidget(ownerValueLabel); ownerLayout->addWidget(groupLabel); ownerLayout->addWidget(groupValueLabel); ownerGroup->setLayout(ownerLayout); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(permissionsGroup); mainLayout->addWidget(ownerGroup); mainLayout->addStretch(1); setLayout(mainLayout); }
ApplicationsTab 类定义
ApplicationsTab 是另一个占位部件,主要用于装饰:
class ApplicationsTab : public QWidget { Q_OBJECT public: explicit ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent = nullptr); };
ApplicationsTab 类实现
ApplicationsTab 不显示任何有用信息,但可用作更复杂示例的模板:
ApplicationsTab::ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent) : QWidget(parent) { QLabel *topLabel = new QLabel(tr("Open with:")); QListWidget *applicationsListBox = new QListWidget; QStringList applications; for (int i = 1; i <= 30; ++i) applications.append(tr("Application %1").arg(i)); applicationsListBox->insertItems(0, applications); QCheckBox *alwaysCheckBox; if (fileInfo.suffix().isEmpty()) alwaysCheckBox = new QCheckBox(tr("Always use this application to " "open this type of file")); else alwaysCheckBox = new QCheckBox(tr("Always use this application to " "open files with the extension '%1'").arg(fileInfo.suffix())); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(topLabel); layout->addWidget(applicationsListBox); layout->addWidget(alwaysCheckBox); setLayout(layout); }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.