逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

#include "BuiltinDialog.h"


#include <QtGui/QTextEdit>

#include <QtGui/QPushButton>

#include <QtGui/QFileDialog>

#include <QtGui/QFontDialog>

#include <QtGui/QColorDialog>

#include <QtGui/QPrintDialog>

#include <QtGui/QInputDialog>

#include <QtGui/QErrorMessage>

#include <QtGui/QProgressDialog>

#include <QtGui/QPageSetupDialog>


#include <QtGui/QGridLayout>

#include <QtGui/QPalette>

#include <QtGui/QColor>

#include <QtGui/QPrinter>

#include <QtGui/QApplication>

#include <QtDebug>


BuiltinDialog::BuiltinDialog(QWidget *parent) :

    QDialog(parent) {

    // Create widgets.

    displayTextEdit = new QTextEdit();

    showFileDialogButton = new QPushButton(QObject::tr("File Dialog"));

    showFontDialogButton = new QPushButton(QObject::tr("Font Dialog"));

    showColorDialogButton = new QPushButton(QObject::tr("Color Dialog"));

    showPrintDialogButton = new QPushButton(QObject::tr("Print Dialog"));

    showInputDialogButton = new QPushButton(QObject::tr("Input Dialog"));

    showErrorDialogButton = new QPushButton(QObject::tr("Error Dialog"));

    showProgressDialogButton = new QPushButton(QObject::tr("Progress Dialog"));

    showPageSetupDialogButton = new QPushButton(

            QObject::tr("Page Setup Dialog"));


    // Lay out widgets.

    QGridLayout *gridLayout = new QGridLayout();

    gridLayout->addWidget(showFileDialogButton, 0, 0, 1, 1);

    gridLayout->addWidget(showFontDialogButton, 0, 1, 1, 1);

    gridLayout->addWidget(showColorDialogButton, 0, 2, 1, 1);

    gridLayout->addWidget(showPrintDialogButton, 1, 0, 1, 1);

    gridLayout->addWidget(showInputDialogButton, 1, 1, 1, 1);

    gridLayout->addWidget(showErrorDialogButton, 1, 2, 1, 1);

    gridLayout->addWidget(showProgressDialogButton, 2, 0, 1, 1);

    gridLayout->addWidget(showPageSetupDialogButton, 2, 1, 1, 1);

    gridLayout->addWidget(displayTextEdit, 3, 0, 3, 3);

    setLayout(gridLayout);


    // Connect signals and slots.

    QObject::connect(showFileDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));

    QObject::connect(showFontDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));

    QObject::connect(showColorDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));

    QObject::connect(showPrintDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));

    QObject::connect(showInputDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));

    QObject::connect(showErrorDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));

    QObject::connect(showProgressDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));

    QObject::connect(showPageSetupDialogButton, SIGNAL(clicked()), this, SLOT(

            buttonsClicked()));


    // Initialize.

    setWindowTitle(QObject::tr("Builtin Dialogs"));

}


void BuiltinDialog::buttonsClicked() {

    QPushButton *button = qobject_cast<QPushButton *> (sender());


    if (button == showFileDialogButton) {

        QString fileName = QFileDialog::getOpenFileName(this, QObject::tr(

                "Open File"), QDir::home().path(), QObject::tr(

                "Images(*.png *.jpg *.gif)"));

        displayTextEdit->setText(fileName);

    } else if (button == showFontDialogButton) {

        bool ok;

        const QFont &font = QFontDialog::getFont(&ok, displayTextEdit->font(),

                this);


        if (ok) {

            displayTextEdit->setFont(font);

        }

    } else if (button == showColorDialogButton) {

        QPalette palette = displayTextEdit->palette();

        const QColor &color = QColorDialog::getColor(palette.color(

                QPalette::Base), this);


        if (color.isValid()) {

            palette.setColor(QPalette::Base, color);

            displayTextEdit->setPalette(palette);

        }

    } else if (button == showPrintDialogButton) {

        QPrinter printer;

        QPrintDialog dialog(&printer, this);

        dialog.setWindowTitle(QObject::tr("Print Dialog"));

        if (QDialog::Accepted == dialog.exec()) {

            displayTextEdit->setText("Printing file.....");

        }

    } else if (button == showInputDialogButton) {

        bool ok;


        QString text = QInputDialog::getText(this, QObject::tr(

                "Input User Name"), QObject::tr("User Name: "),

                QLineEdit::Normal, QDir::home().dirName(), &ok);


        if (ok && !text.isEmpty()) {

            displayTextEdit->setText(text);

        }

    } else if (button == showErrorDialogButton) {

        QErrorMessage box(this);

        box.setWindowTitle(QObject::tr("Error Message"));

        box.showMessage(QObject::tr("There are errors XX."));

        box.showMessage(QObject::tr("There are errors XX."));

        box.showMessage(QObject::tr("There are errors XX."));

        box.showMessage(QObject::tr("There are errors YY."));


        box.exec();

    } else if (button == showProgressDialogButton) {

        int maxValue = 10000;

        QProgressDialog dialog(QObject::tr("Copying files..."), QObject::tr(

                "Cancel"), 0, maxValue, this);

        dialog.setWindowModality(Qt::WindowModal);

        dialog.setWindowTitle(QObject::tr("Progress Dialog"));

        dialog.show();


        for (int i = 0; i < maxValue; ++i) {

            dialog.setValue(i);

            qApp->processEvents();


            if (dialog.wasCanceled()) {

                break;

            }


            qDebug() << i; // #include <QtDebug>

        }


        dialog.setValue(maxValue);

    } else if (button == showPageSetupDialogButton) {

        QPrinter printer;

        QPageSetupDialog dlg(&printer, this);

        dlg.setWindowTitle(QObject::tr("Page Setup"));

        if (QDialog::Accepted == dlg.exec()) {

            displayTextEdit->setText("Page's properties are setupped.");

        }

    }

}

posted on 2009-03-28 02:47 逛奔的蜗牛 阅读(973) 评论(0)  编辑 收藏 引用 所属分类: Qt

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理