QT通用对话框,代码来自《精通QT4编程》,方便日后查询。
main.cpp
- #include
- #include
- #include "dialogbuildin.h"
-
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QTextCodec* pTextCodec = QTextCodec::codecForLocale();
- QTextCodec::setCodecForTr(pTextCodec);
-
- CDialogBuildin dlg;
-
- return dlg.exec();
- }
dialogbuildin.h
- #pragma once
-
- #include
-
- class QTextEdit;
- class QPushButton;
- class CDialogBuildin : public QDialog
- {
- Q_OBJECT
-
- public:
- CDialogBuildin(QWidget *parent = 0);
- virtual ~CDialogBuildin();
-
- private:
- QTextEdit* m_pTextEditDisplay;
- QPushButton* m_pPushBtnColor;
- QPushButton* m_pPushBtnError;
- QPushButton* m_pPushBtnFile;
- QPushButton* m_pPushBtnFont;
- QPushButton* m_pPushBtnInput;
- QPushButton* m_pPushBtnPage;
- QPushButton* m_pPushBtnProgress;
- QPushButton* m_pPushBtnPrint;
-
- private slots:
- void doPushBtn();
- };
dialogbuildin.cpp
- #include "dialogbuildin.h"
- #include
-
- CDialogBuildin::CDialogBuildin(QWidget *parent)
- : QDialog(parent)
- {
- m_pTextEditDisplay = new QTextEdit(tr("QT的标准通用对话框。"));
-
- QGridLayout* pGridLayout = new QGridLayout();
-
- m_pPushBtnColor = new QPushButton(tr("颜色对话框"));
- m_pPushBtnError = new QPushButton(tr("错误消息框"));
- m_pPushBtnFile = new QPushButton(tr("文件对话框"));
- m_pPushBtnFont = new QPushButton(tr("字体对话框"));
- m_pPushBtnInput = new QPushButton(tr("输入对话框"));
- m_pPushBtnPage = new QPushButton(tr("页设置对话框"));
- m_pPushBtnProgress = new QPushButton(tr("进度对话框"));
- m_pPushBtnPrint = new QPushButton(tr("打印对话框"));
-
- pGridLayout->addWidget(m_pPushBtnColor,0,0,1,1);
- pGridLayout->addWidget(m_pPushBtnError,0,1,1,1);
- pGridLayout->addWidget(m_pPushBtnFile,0,2,1,1);
- pGridLayout->addWidget(m_pPushBtnFont,1,0,1,1);
- pGridLayout->addWidget(m_pPushBtnInput,1,1,1,1);
- pGridLayout->addWidget(m_pPushBtnPage,1,2,1,1);
- pGridLayout->addWidget(m_pPushBtnProgress,2,0,1,1);
- pGridLayout->addWidget(m_pPushBtnPrint,2,1,1,1);
- pGridLayout->addWidget(m_pTextEditDisplay,3,0,3,3);
-
- setLayout(pGridLayout);
-
- connect(m_pPushBtnColor,SIGNAL(clicked()),this,SLOT(doPushBtn()));
- connect(m_pPushBtnError,SIGNAL(clicked()),this,SLOT(doPushBtn()));
- connect(m_pPushBtnFile,SIGNAL(clicked()),this,SLOT(doPushBtn()));
- connect(m_pPushBtnFont,SIGNAL(clicked()),this,SLOT(doPushBtn()));
- connect(m_pPushBtnInput,SIGNAL(clicked()),this,SLOT(doPushBtn()));
- connect(m_pPushBtnPage,SIGNAL(clicked()),this,SLOT(doPushBtn()));
- connect(m_pPushBtnProgress,SIGNAL(clicked()),this,SLOT(doPushBtn()));
- connect(m_pPushBtnPrint,SIGNAL(clicked()),this,SLOT(doPushBtn()));
-
- setWindowTitle(tr("内建对话框"));
- resize(400,300);
- }
-
- CDialogBuildin::~CDialogBuildin()
- {
-
- }
-
- void CDialogBuildin::doPushBtn()
- {
- QPushButton* btn = qobject_cast(sender());
- if(btn == m_pPushBtnColor)
- {
-
-
- QPalette palette = m_pTextEditDisplay->palette();
-
-
-
-
-
- const QColor& color = QColorDialog::getColor(palette.color(QPalette::Base),this);
-
-
- if(color.isValid())
- {
-
- palette.setColor(QPalette::Base,color);
- m_pTextEditDisplay->setPalette(palette);
- }
- }
- else if(btn == m_pPushBtnError)
- {
-
- QErrorMessage box(this);
- box.setWindowTitle(tr("错误消息框"));
- box.showMessage(tr("错误消息框实例xx."));
- box.showMessage(tr("错误消息框实例yy."));
- box.showMessage(tr("错误消息框实例zz."));
- box.showMessage(tr("错误消息框实例wu."));
- box.showMessage(tr("错误消息框实例ya."));
- box.exec();
- }
- else if(btn == m_pPushBtnFile)
- {
-
-
-
-
-
-
-
-
- QFileDialog dlg;
- dlg.setViewMode(QFileDialog::Detail);
- dlg.setFileMode(QFileDialog::ExistingFiles);
- dlg.setAcceptMode(QFileDialog::AcceptOpen);
- dlg.setFilter(tr("任何文件(*.*)"";;文本文件(*.txt)"";;XML文件(*.xml)"));
- dlg.setWindowTitle(tr("打开文件"));
- dlg.setResolveSymlinks(false);
- dlg.setNameFilterDetailsVisible(false);
-
- QStringList strListFilePath;
- QString strFilePath;
- if(dlg.exec() == QDialog::Accepted)
- {
- strListFilePath = dlg.selectedFiles();
- strFilePath = strListFilePath.first();
- }
-
- m_pTextEditDisplay->setText(strFilePath);
- }
- else if(btn == m_pPushBtnFont)
- {
-
- bool bok;
- const QFont& font = QFontDialog::getFont(&bok,m_pTextEditDisplay->font(),this,tr("字体对话框"));
-
- if(bok)
- {
-
- m_pTextEditDisplay->setFont(font);
- }
- }
- else if(btn == m_pPushBtnInput)
- {
-
- bool ok;
- QString text = QInputDialog::getText(this,tr("输入对话框"),tr("输入文本:"),QLineEdit::Normal,QDir::home().dirName(),&ok);
-
- if(ok && !text.isEmpty())
- m_pTextEditDisplay->setText(text);
- }
- else if(btn == m_pPushBtnPage)
- {
-
- QPrinter printer;
- QPageSetupDialog dlg(&printer,this);
- dlg.setWindowTitle(tr("页设置对话框"));
- if(dlg.exec() == QDialog::Accepted)
- {
-
- }
- }
- else if(btn == m_pPushBtnProgress)
- {
-
- QProgressDialog progress(tr("正在复制文件..."),
- tr("取消"),
- 0,
- 10000,
- this);
-
- progress.setWindowModality(Qt::WindowModal);
- progress.setWindowTitle(tr("进度对话框"));
- progress.show();
-
- for(int i=0;i<10000;++i)
- {
- progress.setValue(i);
-
-
-
-
-
- QEventLoop EventLoop;
- EventLoop.processEvents(QEventLoop::ExcludeUserInputEvents);
-
- if(progress.wasCanceled())
- break;
-
-
- qDebug() << i;
- }
-
- progress.setValue(10000);
- }
- else if(btn == m_pPushBtnPrint)
- {
-
- QPrinter printer;
- QPrintDialog dlg(&printer,this);
- dlg.setWindowTitle(tr("打印对话框"));
- if(dlg.exec() == QDialog::Accepted)
- {
-
- }
- }
- }
阅读(3678) | 评论(1) | 转发(0) |