Qt的Tab選項卡

tabdlg.h:
 
#ifndef __TABDLG_H__ 
#define __TABDLG_H__ 
 
#include <QDialog> 
 
class QWidget; 
class QFileInfo; 
class QTabWidget; 
class QDialogButtonBox; 
 
//常規選項卡 
class GeneralTab : public QWidget 

        Q_OBJECT 
 
public
        GeneralTab(const QFileInfo &fileInfo, QWidget *parent = 0); 
}; 
 
//許可權限 
class PermissionsTab : public QWidget 

        Q_OBJECT 
 
public
        PermissionsTab(const QFileInfo &fileInfo, QWidget *parent = 0); 
}; 
 
//程序 
class ApplicationsTab : public QWidget 

        Q_OBJECT 
 
public
        ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent = 0);    
}; 
    
//自定義Tab對話框 
class TabDialog:public QDialog 

public
  TabDialog(const QString &fileName, QWidget *parent = 0);    
private
  QTabWidget *tabWidget; 
  QDialogButtonBox *buttonBox; 
}; 
 
 
#endif // __TABDLG_H__ 
 
tabdlg.cpp:
 
#include <QTabWidget> 
#include <QWidget> 
#include <QDialog> 
#include <QFileInfo> 
#include <QVBoxLayout> 
#include <QDialogButtonBox> 
#include <QLabel> 
#include <QLineEdit> 
#include <QDateTime> 
#include <QGroupBox> 
#include <QCheckBox> 
#include <QListWidget> 
 
#include "tabdlg.h" 
 
TabDialog::TabDialog(const QString &fileName, QWidget *parent):QDialog(parent) 

  QFileInfo fileInfo(fileName); 
    
  tabWidget=new QTabWidget; 
  tabWidget->addTab(new GeneralTab(fileName),trUtf8("常規")); 
  tabWidget->addTab(new PermissionsTab(fileName),trUtf8("權限")); 
  tabWidget->addTab(new ApplicationsTab(fileName),trUtf8("應用程序")); 
    
  buttonBox=new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); 
    
  connect(buttonBox,SIGNAL(accepted()),this,SLOT(accept()));//OK 
  connect(buttonBox,SIGNAL(rejected()),this,SLOT(reject()));//Cancel 
    
  QVBoxLayout *mainLayout=new QVBoxLayout; 
  mainLayout->addWidget(tabWidget); 
  mainLayout->addWidget(buttonBox); 
    
  setLayout(mainLayout); 
    
  setWindowTitle(trUtf8("Tab對話框")); 

    
//常規面板類構造函數實現    
GeneralTab::GeneralTab(const QFileInfo &fileInfo, QWidget *parent) 
  :QWidget(parent) 

         QLabel *fileNameLabel = new QLabel(trUtf8("文件名:")); 
         QLineEdit *fileNameEdit = new QLineEdit(fileInfo.fileName());//去掉目錄路徑前綴後的 
 
         QLabel *pathLabel = new QLabel(trUtf8("路徑:")); 
         QLabel *pathValueLabel = new QLabel(fileInfo.absoluteFilePath());//完整的路徑名 
         pathValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);//設置label的樣式 
 
         QLabel *sizeLabel = new QLabel(trUtf8("大小:")); 
         qlonglong size = fileInfo.size()/1024;//size()返回字節大小 
         QLabel *sizeValueLabel = new QLabel(trUtf8("%1 K").arg(size)); 
         sizeValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); 
 
         QLabel *lastReadLabel = new QLabel(trUtf8("上次讀取時間:")); 
         QLabel *lastReadValueLabel = new QLabel(fileInfo.lastRead().toString()); 
         lastReadValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); 
 
         QLabel *lastModLabel = new QLabel(trUtf8("上次修改時間:")); 
         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(const QFileInfo &fileInfo, QWidget *parent) 
  :QWidget(parent) 

    //羣組框 
         QGroupBox *permissionsGroup = new QGroupBox(trUtf8("權限")); 
 
         QCheckBox *readable = new QCheckBox(trUtf8("可讀")); 
         if (fileInfo.isReadable()) 
                 readable->setChecked(true);//勾選 
 
         QCheckBox *writable = new QCheckBox(trUtf8("可寫")); 
         if ( fileInfo.isWritable() ) 
                 writable->setChecked(true); 
 
         QCheckBox *executable = new QCheckBox(trUtf8("可執行")); 
         if ( fileInfo.isExecutable() ) 
                 executable->setChecked(true); 
 
         QGroupBox *ownerGroup = new QGroupBox(trUtf8("所有權")); 
 
         QLabel *ownerLabel = new QLabel(trUtf8("所有者")); 
         QLabel *ownerValueLabel = new QLabel(fileInfo.owner()); 
         ownerValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); 
 
         QLabel *groupLabel = new QLabel(trUtf8("組")); 
         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(trUtf8("打開方式...:")); 
 
         QListWidget *applicationsListBox = new QListWidget; 
         QStringList applications; 
 
         for (int i = 1; i <= 30; ++i) 
                 applications.append(trUtf8("應用程序 %1").arg(i)); 
         applicationsListBox->insertItems(0, applications); 
 
         QCheckBox *alwaysCheckBox; 
 
         if (fileInfo.suffix().isEmpty()) 
                 alwaysCheckBox = new QCheckBox(trUtf8("始終使用該程序" 
                         "打開該類型的文件")); 
         else 
                 alwaysCheckBox = new QCheckBox(trUtf8("始終使用該程序" 
                         "打開此擴展名文件 '%1'").arg(fileInfo.suffix())); 
 
         QVBoxLayout *layout = new QVBoxLayout; 
         layout->addWidget(topLabel); 
         layout->addWidget(applicationsListBox); 
         layout->addWidget(alwaysCheckBox); 
         setLayout(layout); 

 
tabmain.cpp:
 
#include <QApplication> 
#include "tabdlg.h" 
 
int main(int argc,char* argv[]) 

  QApplication app(argc,argv); 
  TabDialog tabdlg("./src/tabmain.cpp"); 
  tabdlg.show(); 
  return app.exec(); 

 
 
截圖:
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章