탭 대화 상자 예제
'탭 대화 상자' 예제는 ` 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"));탭 위젯에는 각각 파일에 대한 정보를 포함하는 세 개의 사용자 정의 위젯이 배치됩니다. 탭 위젯에 위젯이 추가될 때마다 탭 위젯이 해당 위젯들의 부모를 재설정하므로, 이 위젯들은 부모 위젯 없이 생성됩니다.
두 개의 표준 푸시 버튼을 생성하고, 각각을 대화 상자의 적절한 슬롯에 연결합니다:
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 위젯의 정의는 탭 내에 위젯의 내용을 표시하는 데만 관심이 있으므로 간단합니다:
class GeneralTab : public QWidget
{
Q_OBJECT
public:
explicit GeneralTab(const QFileInfo &fileInfo, QWidget *parent = nullptr);
};GeneralTab 클래스 구현
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);
}PermissionsTab 클래스 정의
GeneralTab과 마찬가지로, PermissionsTab은 자식 위젯을 위한 자리 표시자 위젯으로만 사용됩니다:
class PermissionsTab : public QWidget
{
Q_OBJECT
public:
explicit PermissionsTab(const QFileInfo &fileInfo, QWidget *parent = nullptr);
};PermissionsTab 클래스 구현
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 클래스 정의
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);
}© 2026 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.