Qt標準對話方塊實現

2puT發表於2016-08-05
Qt標準對話方塊實現
#include "mywidget.h"
#include "ui_mywidget.h"
#include <QDebug>
#include <QColorDialog>
#include <QTextCodec>
#include "QFileDialog"
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QProgressDialog>
#include <QErrorMessage>
#include <QWizard>



MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
}

MyWidget::~MyWidget()
{
    delete ui;
}

void MyWidget::on_pushButton_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QColor color = QColorDialog::getColor(Qt::red, this, tr("顏色對話方塊")/*, QColorDialog::ShowAlphaChannel*/);
    qDebug() << "color: " << color;
}

void MyWidget::on_pushButton_6_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("檔案對話方塊"), "F:", tr("圖片檔案(* png * jpg);;文字檔案(* txt)"));
    qDebug() << "filenames:" << fileNames;
}

void MyWidget::on_pushButton_2_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    bool ok;
    QFont font = QFontDialog::getFont(&ok, this);
    if (ok) ui->pushButton_2->setFont(font);
    else qDebug() << tr("沒有選擇字型!");

}

void MyWidget::on_pushButton_7_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    bool ok;
    QString string = QInputDialog::getText(this, tr("輸入字串對話方塊"), tr("請輸入使用者名稱: "),QLineEdit::Normal,tr("admin"), &ok);
    if (ok) qDebug() << "string:" << string;
    int value1 = QInputDialog::getInt(this, tr("輸入整數對話方塊"), tr("請輸入-1000到1000之間的數值"), 100, -1000, 1000, 10, &ok);
    if (ok) qDebug() << "value1:" << value1;
    double value2 = QInputDialog::getDouble(this, tr("輸入浮點數對話方塊"), tr("請輸入-1000到1000之間的數值"), 0.00, -1000, 1000, 2, &ok);
    if (ok) qDebug() << "value2:" << value2;
    QStringList items;
    items << tr("條目1") << tr("條目2");
    QString item = QInputDialog::getItem(this, tr("輸入條目對話方塊"), tr("請輸入一個條目"), items, 0, true, &ok);
    if(ok) qDebug() << "item:" << item;


}

void MyWidget::on_pushButton_3_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    int ret1 = QMessageBox::question(this, tr("問題對話方塊"), tr("你瞭解Qt嗎?"), QMessageBox::Yes, QMessageBox::No);
    if (ret1 == QMessageBox::Yes) qDebug() << tr("問題!");

    int ret2 = QMessageBox::information(this, tr("提示對話方塊"), tr("這是Qt書籍!"), QMessageBox::Ok);
    if (ret2 == QMessageBox::Ok) qDebug() << tr("提示!");

    //警告對話方塊
    int ret3 = QMessageBox::warning(this, tr("警告對話方塊"),
                                             tr("不能提前結束!"), QMessageBox::Abort);
    if (ret3 == QMessageBox::Abort) qDebug() << tr("警告!");

    //錯誤對話方塊
    int ret4 = QMessageBox::critical(this, tr("嚴重錯誤對話方塊"),
                                     tr("發現一個嚴重錯誤!現在要關閉所有檔案!"), QMessageBox::YesAll);
    if (ret4 == QMessageBox::YesAll) qDebug() << tr("錯誤!");

    //關於對話方塊
    QMessageBox::about(this, tr("關於對話方塊"), tr("yafeilinux.com致力於Qt及Qt Creator的普及工作!"));
}

void MyWidget::on_pushButton_8_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QProgressDialog dialog(tr("檔案複製進度"), tr("取消"), 0, 50000, this);
    dialog.setWindowTitle(tr("進度對話方塊"));
    dialog.setWindowModality(Qt::WindowModal);
    dialog.show();
    for (int i = 0; i < 50000; i++){
        dialog.setValue(i);
        QApplication::processEvents();
        if (dialog.wasCanceled()) break;
    }

    dialog.setValue(50000);
    qDebug() << tr("複製結束!");



}

void MyWidget::on_pushButton_4_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QErrorMessage *dialog = new QErrorMessage(this);
    dialog->setWindowTitle(tr("錯誤資訊對話方塊"));
    dialog->showMessage(tr("這裡是出錯資訊!"));

}


QWizardPage * MyWidget::createpage1()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("介紹"));
    return page;
}
QWizardPage * MyWidget::createpage2()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("使用者選擇資訊"));
    return page;
}
QWizardPage * MyWidget::createpage3()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("結束"));
    return page;
}
void MyWidget::on_pushButton_5_clicked()
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QWizard wizard(this);
    wizard.setWindowTitle(tr("嚮導對話方塊"));
    wizard.addPage(createpage1());
    wizard.addPage(createpage2());
    wizard.addPage(createpage3());
    wizard.exec();
}
/home/lina/圖片/2016-08-05 09-55-37螢幕截圖.png

相關文章