탭 대화 상자 예제

탭 대화 상자 예제는 QTabWidget 클래스를 사용하여 탭 대화 상자를 구성하는 방법을 보여줍니다.

대화 상자는 애플리케이션이 사용자와 효율적으로 소통할 수 있는 방법을 제공하지만 복잡한 대화 상자는 종종 화면 영역을 너무 많이 차지한다는 문제가 있습니다. 대화 상자에서 여러 개의 탭을 사용하면 접근성을 유지하면서 정보를 여러 카테고리로 나눌 수 있습니다.

탭 대화 상자의 예는 각각 특정 파일에 대한 정보를 포함하는 세 개의 탭과 대화 상자의 내용을 수락하거나 거부하는 데 사용되는 두 개의 표준 푸시 버튼을 제공하는 단일 TabDialog 클래스로 구성됩니다.

TabDialog 클래스 정의

TabDialog 클래스는 QTabWidget 및 두 개의 표준 대화 상자 버튼을 표시하는 QDialog 의 하위 클래스입니다. 클래스 정의에는 클래스 생성자와 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 위젯은 탭 다이얼로그가 전달한 파일에 대한 몇 가지 정보만 표시합니다. 이를 위한 다양한 위젯이 있으며, 세로 레이아웃 내에 배열되어 있습니다:

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 클래스 정의

일반 탭과 마찬가지로 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);
}

예제 프로젝트 @ code.qt.io

© 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.