学习QTabWidget例子
1.
#ifndef GENERALTAB_H
#define GENERALTAB_H
#include <QWidget>
#include <QFileInfo>
class GeneralTab : public QWidget
{
Q_OBJECT
public:
GeneralTab(const QFileInfo &fileInfo, QWidget *parent = 0);
~GeneralTab();
};
#endif
源文件:
#include "generaltab.h"
#include <QDateTime>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
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);
}
如上是QTabWidget中一个页面对象
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui/QDialog>
#include <QtGui/QTabWidget>
#include <QtGui/QDialogButtonBox>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QTabWidget *tab;
QDialogButtonBox *buttonbox;
};
#endif // DIALOG_H
#include "dialog.h"
#include <QFileInfo>
#include <QDialogButtonBox>
#include <QDateTime>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include "generaltab.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
QFileInfo fileInfo("tab.exe");
tab = new QTabWidget;
tab->addTab(new GeneralTab(fileInfo),tr("Info1"));
tab->addTab(new GeneralTab(fileInfo),tr("Info2"));
tab->addTab(new GeneralTab(fileInfo),tr("Info3"));
buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonbox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonbox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tab);
layout->addWidget(buttonbox);
setLayout(layout);
setWindowTitle(tr("Tab Dialog"));
}
Dialog::~Dialog()
{
}