您的位置:首页 > 编程语言 > Qt开发

QT中QTabWidget的使用

2015-12-01 17:11 453 查看
.pro代码:

#-------------------------------------------------
#
# Project created by QtCreator 2015-11-30T15:19:54
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = tabdialog
TEMPLATE = app

SOURCES += main.cpp\
#        mainwindow.cpp\
tabdialog.cpp

#HEADERS  += mainwindow.h
HEADERS  += tabdialog.h

#FORMS    += mainwindow.ui

#QT += widgets

#HEADERS       = tabdialog.h \
#    tabdialog.h
#SOURCES       = main.cpp \
#                tabdialog.cpp \
#    tabdialog.cpp

## install
#target.path = $$[QT_INSTALL_EXAMPLES]/widgets/dialogs/tabdialog
#INSTALLS += target

#wince50standard-x86-msvc2005: LIBS += libcmt.lib corelibc.lib ole32.lib oleaut32.lib uuid.lib commctrl.lib coredll.lib winsock.lib ws2.lib

win32
{
}
DEFINES += _DEBUG
VLD_PATH = C:/Program Files (x86)/Visual Leak Detector
INCLUDEPATH += $VLD_PATH/include
#LIBS += -L $$VLD_PATH/lib/Win32 -lvld
#LIBS += -L $$VLD_PATH/lib/Win32
#LIBS +=  -L C:/Program Files (x86)/Visual Leak Detector/lib/Win32/vld
#LIBS += -Lvld
LIBS += -LC:/Program Files (x86)/Visual Leak Detector/lib/Win32/-lvld
#LIBS+=  -lvld #
#LIBS += -L $VLD_PATH/lib/Win32/vld.lib
#win32 {
#    CONFIG(debug, debug|release)
#    {
##        DEFINES += _DEBUG
#        # vld 2.2 downloaded from http://vld.codeplex.com/  C:\Program Files (x86)\Visual Leak Detector\include
#        VLD_PATH = C:/Program Files (x86)/Visual Leak Detector
#        INCLUDEPATH += $VLD_PATH/include
#        LIBS += -L$VLD_PATH/lib/Win32 -lvld
#    }
#}


tabdialog.h:

#ifndef TABDIALOG_H
#define TABDIALOG_H

#include <QDialog>

class QDialogButtonBox;
class QFileInfo;
class QTabWidget;

class GeneralTab : public QWidget
{
Q_OBJECT

public:
explicit GeneralTab(const QFileInfo &fileInfo, QWidget *parent = 0);
};

class PermissionsTab : public QWidget
{
Q_OBJECT

public:
explicit PermissionsTab(const QFileInfo &fileInfo, QWidget *parent = 0);
};

class ApplicationsTab : public QWidget
{
Q_OBJECT

public:
explicit ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent = 0);
};

class TabDialog : public QDialog
{
Q_OBJECT

public:
explicit TabDialog(const QString &fileName, QWidget *parent = 0);

private:
QTabWidget *tabWidget;
QDialogButtonBox *buttonBox;
};

#endif // TABDIALOG_H


main.cpp:

//#include "mainwindow.h"
//#include <QApplication>

//int main(int argc, char *argv[])
//{
//    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();

//    return a.exec();
//}

#ifdef _DEBUG
#include "vld.h"
#endif

#include <QApplication>

#include "tabdialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString fileName;

if (argc >= 2)
fileName = argv[1];
else
fileName = ".";

TabDialog tabdialog(fileName);
tabdialog.show();

return app.exec();
}


tabdialog.cpp:

#include <QtWidgets>

#include "tabdialog.h"

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, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

//QVBoxLayout *mainLayout = new QVBoxLayout;
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(tabWidget);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);

setWindowTitle(tr("Tab Dialog"));
}

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;
//QHBoxLayout *mainLayout = new QHBoxLayout;
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(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(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);
}




图形:

















当没有给main传参数的时候,argv中只有一个值,就是可执行的exe QT中传参数的方法见上图。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: