LouZhang

导航

标准对话框:StandardDialogs

QT对话框有很多种,有QDialog, QErrorMessage, QInputDialog, QMessageBox, QPrintDialog, QProgressDialog等。。

这个标准对话框的例子要介绍了文件,字体,颜色对话框的使用

main.cpp

#include "standarddialogs.h"

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);

    standarDialogs *standarddialogs = new standarDialogs();
    standarddialogs->show();
    return a.exec();
}
standarddialogs.h
#ifndef STANDARDDIALOGS_H
#define STANDARDDIALOGS_H

#include <QApplication>
#include <QPushButton>
#include <QtGui>

class standarDialogs:public QDialog{
    Q_OBJECT
public:
    standarDialogs(QWidget *parent = 0, Qt::WindowFlags f = 0);
    ~standarDialogs();
public:
    QGridLayout *layout;
    QPushButton *filePushButton;
    QPushButton *colorPushButton;
    QPushButton *fontPushButton;
    QLineEdit *fileLineEdit;
    QLineEdit *fontLineEdit;
    QFrame *colorFrame;
private slots:
    void slotOpenFileDlg();
    void slotOpenColorDlg();
    void slotOpenFontDlg();
};


#endif // STANDARDDIALOGS_H
standarddialogs.cpp
#include "standarddialogs.h"


standarDialogs::standarDialogs(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("fond dialog"));
    fileLineEdit = new QLineEdit;
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    colorFrame->setAutoFillBackground(true);
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("hello linux"));
    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()));
}
standarDialogs::~standarDialogs(){
    ;
}

void standarDialogs::slotOpenFileDlg(){
    QString s = QFileDialog::getOpenFileName(
                this,
                "open file dialog",
                "/",
                "C++ file (*.cpp);;C files(*.c);;Head files(*,h)");
    fileLineEdit->setText(s.toAscii());
}
void standarDialogs::slotOpenColorDlg(){
    QColor color = QColorDialog::getColor(Qt::blue);
    if(color.isValid()){
        colorFrame->setPalette(QPalette(color));
    }
}
void standarDialogs::slotOpenFontDlg(){
    bool ok;
    QFont font = QFontDialog::getFont(&ok);
    if(ok){
        fontLineEdit->setFont(font);
    }
}

风格还是C++的,只不过API全都不懂用了= =

只能慢慢学了。。。

 



posted on 2012-07-28 10:11  louzhang_swk  阅读(375)  评论(0编辑  收藏  举报