这个例子展示了如何使用QTabWidget类实现标签对话框。
对话框为应用程序与用户通信提供了一种有效的方法,但是复杂的对话框会遇到这样的问题,即它们通常占用过多的屏幕区域。通过在对话框中使用多个标签,可以将信息分为不同的类别,同时仍可访问。
标签对话框
示例由一个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"));
该标签项小部件由三个自定义小部件填充,每个小部件都包含有关文件的信息。我们在不使用父窗口小部件的情况下构造了这些窗口中的每一个,因为选项卡窗口小部件会在将它们添加到父窗口小部件时使它们重新定位。
我们创建两个标准按钮,并将每个按钮连接到对话框中的相应槽函数中:
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窗口小部件定义很简单,因为我们只对在选项卡中显示窗口小部件的内容感兴趣:
class GeneralTab : public QWidget
{
Q_OBJECT
public:
explicit GeneralTab(const QFileInfo &fileInfo, QWidget *parent = nullptr);
};
GeneralTab小部件仅显示有关TabDialog传递的文件的一些信息。为此,使用了各种小部件,这些小部件以垂直布局排列:
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);
}
像GeneralTab一样,PermissionsTab只是用作其子级的占位小部件:
class PermissionsTab : public QWidget
{
Q_OBJECT
public:
explicit PermissionsTab(const QFileInfo &fileInfo, QWidget *parent = nullptr);
};
PermissionsTab显示有关文件访问信息的信息,并在以嵌套布局排列的小部件中显示文件许可权和所有者的详细信息:
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是另一个占位符小部件,大部分是装饰的:
class ApplicationsTab : public QWidget
{
Q_OBJECT
public:
explicit ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent = nullptr);
};
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);
}
C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\widgets\dialogs\tabdialog
https://doc.qt.io/qt-5/qtwidgets-dialogs-tabdialog-example.html