这一部分讲的都是一些对话框的使用,和穿衣一样,不同的地方穿不一样的衣服,对话框也是,不同的场合用不同的对话框。
一个简单的实例,三个Button,两个LineEdit,以及一个Frame
在这个程序中涉及到几个函数的使用:
1、
获得用户选择的文件名
void Dialog::slotOpenFile()
{
QString s = QFileDialog::getOpenFileName(this,"open file dialog","/","C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
fileLineEdit->setText(s.toCaseFolded());
}
这是一个函数,用到了getOpenFileName()静态函数,这个函数是QFileDialog类中的,返回用户选择的文件名。与此相关的函数还有一些,getSaveFileName()获得用户保存的文件名,getExistingDirectory()获得用户选择的已经存在的目录名,getOpenFileNames获得用户选择的文件名列表。
2、获得用户选择的颜色值:
void Dialog::slotOpenColor(){
QColor color = QColorDialog::getColor(Qt::blue);if(color.isValid()){colorFrame->setPalette(QPalette(color));}}
3、获得用户选择的字体:
void Dialog::slotOpenFont(){
bool ok;QFont font = QFontDialog::getFont(&ok);if(ok){fontLineEdit->setFont(font);}}
完整代码:
dialog.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include <QPushButton>#include <QLineEdit>#include <QFrame>#include <QGridLayout>#include <QString>#include <QFileDialog>//选择文件对话框#include <QColorDialog>//选择颜色对话框#include <QFontDialog>//显示文本对话框#include <Qt>namespace Ui {class Dialog;}
class Dialog : public QDialog{
Q_OBJECTpublic:QGridLayout* layout;QPushButton* fileButton;QPushButton* colorButton;QPushButton* fontButton;QLineEdit* fileLineEdit;QLineEdit* fontLineEdit;QFrame* colorFrame;private slots:void slotOpenFile();void slotOpenColor();void slotOpenFont();public:explicit Dialog(QWidget *parent = 0);~Dialog();private:Ui::Dialog *ui;};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog){
ui->setupUi(this);setWindowTitle(tr("standar dialog"));layout = new QGridLayout(this);fileButton = new QPushButton;fileButton->setText(tr("文本选择框"));colorButton = new QPushButton;colorButton->setText(tr("颜色选择框"));fontButton = new QPushButton;fontButton->setText(tr("显示文本"));fileLineEdit = new QLineEdit;//点击相关的按钮之后,将会显示一个颜色选择框,在这个颜色选择框中选择颜色。colorFrame = new QFrame;colorFrame->setFrameShape(QFrame::Box);colorFrame->setAutoFillBackground(true);//设置显示的文字fontLineEdit = new QLineEdit;fontLineEdit->setText(tr("Hello world"));layout->addWidget(fileButton,0,0);layout->addWidget(fileLineEdit,0,1);layout->addWidget(colorButton,1,0);layout->addWidget(colorFrame,1,1);layout->addWidget(fontButton,2,0);layout->addWidget(fontLineEdit,2,1);connect(fileButton,SIGNAL(clicked()),this,SLOT(slotOpenFile()));connect(colorButton,SIGNAL(clicked()),this,SLOT(slotOpenColor()));connect(fontButton,SIGNAL(clicked()),this,SLOT(slotOpenFont()));}
//选择文件对话框
void Dialog::slotOpenFile(){
QString s = QFileDialog::getOpenFileName(this,"open file dialog","/","C++ files(*.cpp);;C files(*.c);;Head files(*.h)");fileLineEdit->setText(s.toCaseFolded());
}
//选择颜色对话框
void Dialog::slotOpenColor(){
QColor color = QColorDialog::getColor(Qt::blue);if(color.isValid()){colorFrame->setPalette(QPalette(color));}}
//显示文本对话框
void Dialog::slotOpenFont(){
bool ok;QFont font = QFontDialog::getFont(&ok);if(ok){fontLineEdit->setFont(font);}}
Dialog::~Dialog(){
delete ui;}