//main.cpp
#include <QApplication> #include <QTranslator> #include <QProcess> #include <QLocale> #include <QLibraryInfo> #include <QDebug> #include <QtGui/QtGui> #include"mybuiltindlg.h" int main(int argc,char* argv[]) { QApplication app(argc, argv); QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030")); MyBuiltinDlg mydialog; QMessageBox box; QTranslator translator; { QStringList environment = QProcess::systemEnvironment(); QString str; bool bFinded =false; foreach(str,environment) { if(str.startsWith("QTDIR=")) { bFinded =true; break; } } if(bFinded) { str=str.mid(6); bFinded =translator.load("qt_"+QLocale::system().name(), str.append("/translations")); if(bFinded) qApp->installTranslator(&translator); else { // qDebug() << QObject::trUtf8("没有中文"); box.setWindowTitle("warning"); box.setIcon(QMessageBox::Warning); box.setText("没有支持中文的国际化翻译软件!!"); box.setStandardButtons(QMessageBox::Yes); box.exec(); } } else { //qDebug() << QObject::trUtf8("必须设置"); box.setWindowTitle("warning"); box.setIcon(QMessageBox::Warning); box.setText("必须设置QTDIR环境变量!!"); box.setStandardButtons(QMessageBox::Yes); box.exec(); exit(1); } } return mydialog.exec(); }
#ifndef CBUILTINDLG_H #define CBUILTINDLG_H #include <QDialog> class QTextEdit; class QPushButton; class MyBuiltinDlg : public QDialog { Q_OBJECT public: explicit MyBuiltinDlg(QWidget *parent = 0); virtual ~MyBuiltinDlg(){} signals: public slots: void doPushBtn(); private: QTextEdit* displayTextEdit; QPushButton* colorPushBtn; QPushButton* errorPushBtn; QPushButton* filePushBtn; QPushButton* fontPushBtn; QPushButton* inputPushBtn; QPushButton* pagePushBtn; QPushButton* progressPushBtn; QPushButton* printPushBtn; }; #endif // CBUILTINDLG_H
#include "mybuiltindlg.h" #include<QtGui/QtGui> MyBuiltinDlg::MyBuiltinDlg(QWidget *parent) : QDialog(parent) { displayTextEdit =new QTextEdit(trUtf8("Qt标准通用对话框!!")); QGridLayout* gridLayout = new QGridLayout; colorPushBtn = new QPushButton(trUtf8("clor")); errorPushBtn = new QPushButton(trUtf8("error")); filePushBtn = new QPushButton(trUtf8("file")); fontPushBtn = new QPushButton(trUtf8("font")); inputPushBtn = new QPushButton(trUtf8("input")); pagePushBtn = new QPushButton(trUtf8("page")); progressPushBtn = new QPushButton(trUtf8("progress")); printPushBtn = new QPushButton(trUtf8("print")); gridLayout->addWidget(colorPushBtn,0,0,1,1); gridLayout->addWidget(errorPushBtn,0,1,1,1); gridLayout->addWidget(filePushBtn,0,2,1,1); gridLayout->addWidget(fontPushBtn,1,0,1,1); gridLayout->addWidget(inputPushBtn,1,1,1,1); gridLayout->addWidget(pagePushBtn,1,2,1,1); gridLayout->addWidget(progressPushBtn,2,0,1,1); gridLayout->addWidget(printPushBtn,2,1,1,1); gridLayout->addWidget(displayTextEdit,3,0,3,3); setLayout(gridLayout); connect(colorPushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); connect(errorPushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); connect(filePushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); connect(fontPushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); connect(inputPushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); connect(pagePushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); connect(progressPushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); connect(printPushBtn,SIGNAL(clicked()),this,SLOT(doPushBtn())); setWindowTitle(trUtf8("内建对话框")); resize(400,300); } void MyBuiltinDlg::doPushBtn() { /* *槽函数的开头是获取发送信号的QPushButton对象指针。函数QObject::sender() *返回发送信号的对象的指针,返回类型为QObject*, qobject_cast是模板函数, *这里C++不太懂, */ QPushButton* btn = qobject_cast<QPushButton*>(sender()); if(btn == colorPushBtn) {//颜色对话框 //QTextEdit::palette()获取文本编辑框对象的调色板 QPalette palette = displayTextEdit->palette(); /* *调用QColorDialog::getColor()函数创建并显示一个模态的颜色对话框 *并返回用户选择的颜色对象的常量引用(对临时对象的引用是无效的,必须使 *用常量引用)赋值变量color *颜色对话框的颜色初始化为palette.color(QPalette::Base),即文本编辑 *框的背景色,父窗口部件为this */ const QColor& color = QColorDialog::getColor(palette.color(QPalette::Window),this); if(color.isValid()) { /* *color.isValid()判断函数是否有效 *palette.setColor有两个参数第一个参数是指定调色板的角色 ×该函数会影响到所有的三个颜色组 ×第二个参数是指定应该设置的颜色 *重新设置文本编辑框的调色板 */ palette.setColor(QPalette::Base,color); displayTextEdit->setPalette(palette); } } else if(btn == errorPushBtn) { //错误对话框 QErrorMessage box(this); box.setWindowTitle(trUtf8("错误消息框")); box.showMessage(trUtf8("错误消息框实例xx")); box.showMessage(trUtf8("错误消息框实例yy")); box.showMessage(trUtf8("错误消息框实例zz")); box.showMessage(trUtf8("错误消息框实例xx")); box.showMessage(trUtf8("错误消息框实例xx")); box.exec(); } else if(btn ==filePushBtn) { //文件对话框 QString filename = QFileDialog::getOpenFileName( this, trUtf8("打开文件"), "/home/pillar", trUtf8("任何文件(*.*)" ";;文本文件(*.txt)" ";;XML文件(*.xml)")); displayTextEdit->setText(filename); } else if(btn == fontPushBtn) { bool ok; const QFont& font = QFontDialog::getFont(&ok, displayTextEdit->font(), this, trUtf8("字体对话框")); if(ok) { displayTextEdit->setFont(font); } } else if(btn == inputPushBtn) { bool ok; QString text = QInputDialog::getText(this, trUtf8("输入对话框"), trUtf8("输入文本"), QLineEdit::Normal, QDir::home().dirName(), &ok); if(ok && !text.isEmpty()) displayTextEdit->setText(text); } else if(btn == pagePushBtn) { QPrinter printer; QPageSetupDialog dlg(&printer,this); dlg.setWindowTitle(trUtf8("页设置对话框")); if(dlg.exec()== QDialog::Accepted) { } } else if(btn == progressPushBtn) { QProgressDialog progree(trUtf8("正在复制....."), trUtf8("cancel"), 0, 10000,this); progree.setWindowModality(Qt::WindowModal); progree.setWindowTitle(trUtf8("进度对话框")); progree.show(); for(int i = 0;i < 10000;i++) { progree.setValue(i); qApp->processEvents();//保证事件循环还可以处理其他事件 if(progree.wasCanceled()) break; qDebug() << i; } progree.setValue(10000); } else if(btn == printPushBtn) { QPrinter printer; QPrintDialog dlg(&printer,this); dlg.setWindowTitle(trUtf8("打印对话框")); if(dlg.exec() == QDialog::Accepted) { } } }