Qt: 内建对话框(各种对话框都有了,且用到了qobject_cast解析sender的技术)

#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.");

}

}

}

http://www.cppblog.com/biao/archive/2009/03/28/78105.html

时间: 2024-08-10 06:47:54

Qt: 内建对话框(各种对话框都有了,且用到了qobject_cast解析sender的技术)的相关文章

内建对象

内建对象是指由ECMAScript事先提供的.不依赖于宿主环境的对象,这些对象在程序运行之前就已经存在了.可以直接在程序中任何地方任何时候拿来使用. 1. 内建全局单例对象   Global.Math.JSON. 在整个执行环境中只有一个对象实例,这些对象没有内部属性[[Construct]]和[[Call]],所以不能使用new来创建,也不能作为函数来调用,而是直接使用对象名称来引用其属性和方法(对于全局对象,则可以直接使用属性和方法名). 1.1  常用内建全局单例对象方法 Global对象

QT 使用QSS实现圆角对话框

QT 使用QSS实现圆角对话框 圆角对话框有很多办法,一般是用代码重绘的方式,比较复杂,这一个高仿360的界面给的代码,比较复杂,放入我的QDialog的窗口总是不生效: MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); setWindowFlags(Qt::FramelessWindowHint);//隐藏边框和标题栏 //生成一张位

QT学习二 按钮新建对话框

mainwindow新建方法MainWindowOpen打开一个新的对话框 mainWindow.h 1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 6 namespace Ui { 7 class MainWindow; 8 } 9 10 class MainWindow : public QMainWindow 11 { 12 Q_OBJECT 13 14 public: 15 expli

QT+ 使用标准对话框+关于对话框+问题对话框+文件对话框

#include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAction> #include <QDialog> #include<QDebug> #include <QMessageBox> #include <QFileDialog> MainWindow::MainWindow(QWidget *parent) :

ASP基础教程:ASP内建对象Response

在上一篇中作者给大家详细介绍了 ASP 内建对象之一 Request 的使用方法,相信各位通过一系列的实践已经能够熟练掌握,本篇将继续给大家介绍 ASP 的另一个内建对象 Response. 最近,有很多朋友来“妹儿”催我加快 ASP 篇一文的写作速度,并急切地询问哪里有关于 ASP 的教材.我深深地被大家的学习热情所打动,因此决定将自己平时所搜集的一些 ASP 信息资料拿出来和大家共享,也希望所有的朋友能慷慨地将自己搜集的有关 ASP 的资料告诉作者,谢谢.由于目前国内有关 ASP 的中文教材

ASP基础教程:ASP内建对象Server

通过前九篇的理论和实践,相信大家已经对 ASP 有了系统的了解,虽然至今为止,我们只学了 ASP 的 4 个内建对象,但已经完全能够写出一些实用的小程序了.今天,作者将继续给大家讲解最后一个 ASP 内建对象——Server. 在开始本次课程之前,我仍要在这里回答一些朋友们提出的比较普遍的问题.最近仍有不少朋友来信问我,如何构建服务器端的 Active Server Page 环境.我想可能是我在前几篇中没有讲清楚,因此,在本篇的开头有必要把这个问题详细阐述一遍. ASP 的应用完全是基于 Mi

python高级编程之(类级):子类内建类型

# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一(请访问:https://www.python.org/download/releases/2.2.3/descintro(可能已经不存在了))-这使内建类型的子类化成为可能,并且添加一个新内建类型object #用于所有内建类的公共祖先 #展示一个名为distinctdict类的代码,与平常的dic

Python内建方法

参考: https://docs.python.org/3.4/library/functions.html https://docs.python.org/2/library/functions.html http://blog.csdn.net/jgood/article/details/4371991 以上链接分别为Python官网的3.4版本的内建方法说明.2.X(指2.6和2.7)版本的内建方法说明.以及JGood对2.X版本的内建方法说明的翻译. abs(x) 返回一个数的绝对值.参

任务内建消息队列——OSTaskQ???()

一.知识背景 实际应用中,多个任务同时等待一个消息队列的情况很少见,也就是说OSQ???()用的并不多,因此,在uC/OS-III 中,每一个任务都有其内建的消息队列.用户可以通过外部消息队列直接发送消息给任务. 这个特性不仅简化了代码, 还提高了效率.如下示意图, uC/OS-III 中与任务消息队列相关的服务都是以 OSTask???()开头的.设置 OS_CFG.H 中的 OS_CFG_TASK_EN 使能任务的消息队列服务.与任务消息队列相关的代码在 OS_TASK.C 中. 当用户清楚