日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口

發(fā)布時(shí)間:2024/9/27 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


1 新建一個(gè)空項(xiàng)目

A 編寫 .pro文件

QT += gui widgets

?

HEADERS += \

??? MyDialog.h

?

SOURCES += \

??? MyDialog.cpp

B 編寫MyDialog.h

#ifndef MYDIALOG_H

#define MYDIALOG_H

?

#include <QDialog>

?

class MyDialog:public QDialog

{

??? Q_OBJECT

public:

??? explicit MyDialog(QWidget *parent = 0);

?

??? QString _strDir;

??? void paintEvent(QPaintEvent *);

?

signals:

?

public slots:

??? void slotButtonClick();

};

?

#endif // MYDIALOG_H

C? 編寫:MyDialog.cpp

#include "MyDialog.h" #include <QPushButton> #include <QDebug> #include <QFileDialog> #include <QFileInfo> ? #include <QColorDialog> #include <QFontDialog> #include <QMessageBox> #include <QPainter> ? #include <QApplication> ? MyDialog::MyDialog(QWidget *parent) : ??? QDialog(parent) { ??? QPushButton* button = new QPushButton("Click me",this); ??? connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClick())); } ? void MyDialog::slotButtonClick() { #if 0 ??? QDialog* dlg = new QDialog; ??? int ret; ??? QPushButton* button = new QPushButton(dlg); ??? connect(button, SIGNAL(clicked()), dlg, SLOT(reject())); ? ??? /* ???? * 在模態(tài)對(duì)話框中,exec有自己的消息循環(huán),并且把app的消息循環(huán)接管了 ???? * 如果Dialog是通過exec來顯示,那么可以通過accepted或者rejected來關(guān)閉 ???? * 窗口,如果Dialog是通過show來顯示,那么可以通過close來關(guān)閉窗口, ???? * 這個(gè)和QWidget一樣的 ???? * ???? * 有許多特殊的dailog:文件選擇,MessageBox,顏色選擇,字體選擇,打印預(yù)覽,打印 ???? */ ??? ret = dlg->exec(); ??? if(ret == QDialog::Accepted) ??? { ??????? qDebug() << "accepted"; ??? } ??? if(ret == QDialog::Rejected) ??? { ??????? qDebug() << "rejected"; } //上面的運(yùn)行結(jié)果如下: #endif #if 0 ??? //通過下面的方式打開保存文件 QString strFilename = QFileDialog::getSaveFileName( NULL, ????????????????????????? "Select file for save", ????????????????????????? _strDir, ????????????????????????? "pic file (*.png *.jpg)"); //運(yùn)行結(jié)果: #endif #if 0 ????//打開一個(gè)文件 #endif #if 0 ???? //選擇一個(gè)存在的文件夾 ??? QString strFilename = QFileDialog::getExistingDirectory(); ??? if(strFilename.isEmpty()) ??? { ??????? qDebug() << "select none"; ??????? return; ??? } ? ??? qDebug() << strFilename; ??? QFileInfo fileInfo(strFilename); ??? _strDir = fileInfo.filePath(); ? #endif #if 0 //顏色選擇框 QColorDialog color; ??? color.exec(); QColor c = color.selectedColor(); #endif #if 0 //字體選擇器 QFontDialog fontDialog; ??? fontDialog.exec(); QFont font = fontDialog.selectedFont(); #endif #if 0 //MessageBox,消息提示窗口 ??? int ret = QMessageBox::question(this, "????", "realy do .......", ????????????? QMessageBox::Yes| QMessageBox::No| ????????????? QMessageBox::YesAll| QMessageBox::NoAll); ??? if(ret == QMessageBox::Yes) ??? { ???????? qDebug() << "user select yes"; ??? } ??? if(ret == QMessageBox::No) ??? { ???????? qDebug() << "user select no"; ??? } #endif } ? void MyDialog::paintEvent(QPaintEvent *) { ??? QPainter p(this); ??? p.drawLine(QLine(0,0,200,200)); } ? int main(int argc,char* argv[]) { ??? QApplication app(argc,argv); ? ??? MyDialog dlg; ??? dlg.show(); ? ??? return app.exec(); }

?

總結(jié)

以上是生活随笔為你收集整理的2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。