天天看點

QT學習——執行個體1:标準對話框

功能:QT 實作3個标準對話框,文本對話框、顔色對話框、字型設定對話框。

圖示:

QT學習——執行個體1:标準對話框

圖1 基本對話框

QT學習——執行個體1:标準對話框

圖2 文本對話框

QT學習——執行個體1:标準對話框

圖3 顔色對話框

QT學習——執行個體1:标準對話框

圖4 字型設定對話框

分析:

标準對話框頭檔案代碼:

#ifndef STANDARDDIALOGS_H

#define STANDARDDIALOGS_H

#include <QtGui>

class StandardDialogs : public QDialog

{

    Q_OBJECT

public:

    StandardDialogs( QWidget *parent=0, Qt::WindowFlags  f=0 );

public:

    QGridLayout *layout;

    QPushButton *filePushButton;

    QPushButton *colorPushButton;

    QPushButton *fontPushButton;

    QLineEdit *fileLineEdit;  

    QLineEdit *fontLineEdit; 

    QFrame *colorFrame;

private slots:

    void slotOpenFileDlg();

    void slotOpenColorDlg();

    void slotOpenFontDlg();

};

#endif

标準對話框CPP檔案代碼:

#include "standarddialogs.h"

StandardDialogs::StandardDialogs( QWidget *parent, Qt::WindowFlags  f )//構造函數

    : QDialog( parent, f )

    setWindowTitle(tr("Standard Dialogs"));

    layout = new QGridLayout( this ); 

    filePushButton = new QPushButton();

    filePushButton->setText(tr("File Dialog"));

    colorPushButton = new QPushButton();

    colorPushButton->setText(tr("Color Dialog"));

    fontPushButton = new QPushButton( );

    fontPushButton->setText(tr("Font Dialog"));

    fileLineEdit = new QLineEdit();

    colorFrame = new QFrame();

    colorFrame->setFrameShape( QFrame::Box );

    colorFrame->setAutoFillBackground(true);

    fontLineEdit = new QLineEdit( );

    fontLineEdit->setText(tr("Hello World"));

    layout->addWidget( filePushButton, 0, 0 ); 

    layout->addWidget( fileLineEdit, 0, 1 );

    layout->addWidget( colorPushButton, 1, 0 );

    layout->addWidget( colorFrame, 1, 1 );

    layout->addWidget( fontPushButton, 2, 0 );

    layout->addWidget( fontLineEdit, 2, 1 );

    layout->setMargin(15);

    layout->setSpacing(10);

    connect(filePushButton,SIGNAL(clicked()),this,SLOT(slotOpenFileDlg()));

    connect(colorPushButton,SIGNAL(clicked()),this,SLOT(slotOpenColorDlg()));

    connect(fontPushButton,SIGNAL(clicked()),this,SLOT(slotOpenFontDlg()));

}                             

void StandardDialogs::slotOpenFileDlg()

{                             

    QString s = QFileDialog::getOpenFileName(              

 this, "open file dialog",

        "/",

        "C++ files (*.cpp);;C files (*.c);;Head files (*.h)");           

    fileLineEdit->setText( s.toAscii() );        

}                                                          

void StandardDialogs::slotOpenColorDlg()                   

{                                                          

    QColor color = QColorDialog::getColor (Qt::blue);   

    if(color.isValid())

    {

  colorFrame->setPalette(QPalette(color));

    }

}                                                          

void StandardDialogs::slotOpenFontDlg()                    

{                            

    bool ok;                 

    QFont font = QFontDialog::getFont( &ok );

    if( ok )

    {

     fontLineEdit->setFont( font );

    }

}

重點:槽函數1:QFileDialog::getOpenFileName()

為了說明QFileDialog::getOpenFileName()函數的用法,還是先把函數簽名放在這裡:  

QT學習——執行個體1:标準對話框

QString QFileDialog::getOpenFileName (           QWidget * parent = 0,           const QString & caption = QString(),           const QString & dir = QString(),           const QString & filter = QString(),           QString * selectedFilter = 0,           Options options = 0 )   第一個參數parent,用于指定父元件。注意,很多Qt元件的構造函數都會有這麼一個parent參數,并提供一個預設值0; 第二個參數caption,是對話框的标題 第三個參數dir,是對話框顯示時預設打開的目錄,"." 代表程式運作目錄,"/" 代表目前盤符的根目錄(Windows,Linux下/就是根目錄了),也可以是平台相關的,比如"C:\\"等; 第四個參數filter,是對話框的字尾名過濾器,比如我們使用"Image Files(*.jpg *.png)"就讓它隻能顯示字尾名是jpg或者png的檔案。如果需要使用多個過濾器,使用";;"分割,比如"JPEG Files     (*.jpg);;PNG Files(*.png)"; 第五個參數selectedFilter,是預設選擇的過濾器; 第六個參數options,是對話框的一些參數設定,比如隻顯示檔案夾等等,它的取值是enum QFileDialog::Option,每個選項可以使用 | 運算組合起來。

槽函數2:QColorDialog::getColor ( )

QColorDialog::getColor ( const QColor & initial = white, QWidget * parent = 0, const char * name = 0 ) [靜态] ;

QColorDialog::getColor()調用了QColorDialog的static函數getColor()。這個函數有兩個參數,第一個是QColor類型,是對話框打開時預設選擇的顔色,第二個是它的parent。

槽函數3:QFontDialog::getFont ( )

QFontDialog::getFont ( bool * ok, const QFont & initial, QWidget * parent = 0, const char * name = 0 ) [靜态] ;

主函數CPP檔案代碼:

#include "standarddialogs.h"

#include <QApplication>

int main( int argc, char **argv )

{

    QFont font("ZYSong18030",12);

    QApplication::setFont(font);

    QApplication a( argc, argv );

    QTranslator translator(0);

    translator.load("standarddialogs_zh",".");

    a.installTranslator(&translator);   

    StandardDialogs *standarddialogs = new StandardDialogs();

    standarddialogs->show();

    return a.exec();

}