一、标准对话框
1. QFileDialog 对话框
功能 :提供文件选择对话框,方便用户选择文件或目录。
# include <QApplication>
# include <QFileDialog>
# include <QMessageBox>
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
QString fileName = QFileDialog :: getOpenFileName ( nullptr , "Open File" , QDir :: homePath ( ) , "All Files (*.*)" ) ;
if ( ! fileName. isEmpty ( ) ) {
QMessageBox :: information ( nullptr , "File Selected" , "You selected: " + fileName) ;
}
return app. exec ( ) ;
}
代码解释 :
QFileDialog::getOpenFileName
用于打开文件选择对话框,参数依次为:父窗口、对话框标题、起始目录、文件过滤器。如果用户选择了文件(fileName
不为空),将弹出消息框显示选择的文件路径。
2. QColorDialog 对话框
# include <QApplication>
# include <QColorDialog>
# include <QPushButton>
# include <QWidget>
# include <QVBoxLayout>
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
QWidget window;
QVBoxLayout * layout = new QVBoxLayout ( & window) ;
QPushButton * button = new QPushButton ( "Choose Color" ) ;
QObject :: connect ( button, & QPushButton:: clicked, [ & ] ( ) {
QColor color = QColorDialog :: getColor ( Qt:: white, & window) ;
if ( color. isValid ( ) ) {
QMessageBox :: information ( & window, "Color Selected" , "Color: " + color. name ( ) ) ;
}
} ) ;
layout-> addWidget ( button) ;
window. show ( ) ;
return app. exec ( ) ;
}
代码解释 :
创建一个按钮,点击按钮时弹出 QColorDialog
。 使用 QColorDialog::getColor
获取用户选择的颜色,若有效则显示颜色名称。
3. QFontDialog 对话框
# include <QApplication>
# include <QFontDialog>
# include <QPushButton>
# include <QWidget>
# include <QVBoxLayout>
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
QWidget window;
QVBoxLayout * layout = new QVBoxLayout ( & window) ;
QPushButton * button = new QPushButton ( "Choose Font" ) ;
QObject :: connect ( button, & QPushButton:: clicked, [ & ] ( ) {
QFont font = QFontDialog :: getFont ( nullptr ) ;
if ( font. isValid ( ) ) {
QMessageBox :: information ( & window, "Font Selected" , "Font: " + font. family ( ) ) ;
}
} ) ;
layout-> addWidget ( button) ;
window. show ( ) ;
return app. exec ( ) ;
}
代码解释 :
创建一个按钮,点击按钮时弹出 QFontDialog
。 使用 QFontDialog::getFont
获取用户选择的字体,若有效则显示字体族名称。
4. QProgressDialog 对话框
功能 :显示进度条,常用于长时间操作时向用户显示进度。
# include <QApplication>
# include <QProgressDialog>
# include <QTimer>
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
QProgressDialog progress ( "Progress" , "Cancel" , 0 , 100 ) ;
progress. setWindowTitle ( "Progress Dialog" ) ;
QTimer timer;
int value = 0 ;
QObject :: connect ( & timer, & QTimer:: timeout, [ & ] ( ) {
value++ ;
progress. setValue ( value) ;
if ( value >= 100 ) {
timer. stop ( ) ;
progress. setValue ( 100 ) ;
}
} ) ;
timer. start ( 100 ) ;
progress. exec ( ) ;
return app. exec ( ) ;
}
代码解释 :
创建 QProgressDialog
并设置范围。 使用 QTimer
模拟进度更新,每秒更新一次进度,直到达到 100。
5. QMessageBox 消息对话框
# include <QApplication>
# include <QMessageBox>
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
QMessageBox :: information ( nullptr , "Message" , "This is an information message" ) ;
QMessageBox :: question ( nullptr , "Question" , "Do you want to continue?" , QMessageBox:: Yes | QMessageBox:: No) ;
return app. exec ( ) ;
}
代码解释 :
QMessageBox::information
显示信息消息。QMessageBox::question
显示询问对话框,用户可选择是或否。
二、设计和使用自定义对话框
1. QDialog 类
# include <QApplication>
# include <QDialog>
# include <QVBoxLayout>
# include <QPushButton>
class CustomDialog : public QDialog
{
public :
CustomDialog ( QWidget * parent = nullptr ) : QDialog ( parent)
{
QVBoxLayout * layout = new QVBoxLayout ( this ) ;
QPushButton * button = new QPushButton ( "Close" , this ) ;
connect ( button, & QPushButton:: clicked, this , & QDialog:: accept) ;
layout-> addWidget ( button) ;
}
} ;
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
CustomDialog dialog;
if ( dialog. exec ( ) == QDialog:: Accepted) {
}
return app. exec ( ) ;
}
代码解释 :
自定义 CustomDialog
类,包含一个按钮。 点击按钮调用 accept
关闭对话框,根据 exec
的返回值判断对话框的关闭状态。
2. TDialogHeaders 对话框设计和使用(示例)
# include <QApplication>
# include <QDialog>
# include <QVBoxLayout>
# include <QPushButton>
# include <QHeaderView>
class TDialogHeaders : public QDialog
{
public :
TDialogHeaders ( QWidget * parent = nullptr ) : QDialog ( parent)
{
QVBoxLayout * layout = new QVBoxLayout ( this ) ;
QPushButton * button = new QPushButton ( "Apply Headers" , this ) ;
connect ( button, & QPushButton:: clicked, this , & QDialog:: accept) ;
layout-> addWidget ( button) ;
}
} ;
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
TDialogHeaders dialog;
if ( dialog. exec ( ) == QDialog:: Accepted) {
}
return app. exec ( ) ;
}
代码解释 :
创建自定义对话框,可添加 QHeaderView
等组件进行更多自定义操作。
3. TDialogLocate 对话框设计和使用(示例)
# include <QApplication>
# include <QDialog>
# include <QVBoxLayout>
# include <QPushButton>
# include <QLineEdit>
class TDialogLocate : public QDialog
{
public :
TDialogLocate ( QWidget * parent = nullptr ) : QDialog ( parent)
{
QVBoxLayout * layout = new QVBoxLayout ( this ) ;
QLineEdit * lineEdit = new QLineEdit ( this ) ;
QPushButton * button = new QPushButton ( "Locate" , this ) ;
connect ( button, & QPushButton:: clicked, this , & QDialog:: accept) ;
layout-> addWidget ( lineEdit) ;
layout-> addWidget ( button) ;
}
} ;
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
TDialogLocate dialog;
if ( dialog. exec ( ) == QDialog:: Accepted) {
}
return app. exec ( ) ;
}
代码解释 :
创建自定义对话框,包含 QLineEdit
和按钮,可根据输入进行相应操作。
三、多窗口应用程序设计
1. 窗口类重要特性的设置
# include <QApplication>
# include <QWidget>
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
QWidget window1;
window1. setWindowTitle ( "Window 1" ) ;
window1. resize ( 300 , 200 ) ;
window1. move ( 100 , 100 ) ;
window1. show ( ) ;
QWidget window2;
window2. setWindowTitle ( "Window 2" ) ;
window2. resize ( 300 , 200 ) ;
window2. move ( 500 , 100 ) ;
window2. show ( ) ;
return app. exec ( ) ;
}
2. 多窗口应用程序设计
# include <QApplication>
# include <QMainWindow>
# include <QPushButton>
# include <QDialog>
class MainWindow : public QMainWindow
{
public :
MainWindow ( )
{
QPushButton * button = new QPushButton ( "Open Dialog" , this ) ;
setCentralWidget ( button) ;
connect ( button, & QPushButton:: clicked, this , [ = ] ( ) {
QDialog dialog ( this ) ;
dialog. setWindowTitle ( "Dialog" ) ;
dialog. exec ( ) ;
} ) ;
}
} ;
int main ( int argc, char * argv[ ] )
{
QApplication app ( argc, argv) ;
MainWindow mainWindow;
mainWindow. show ( ) ;
return app. exec ( ) ;
}
设计用例
可以将上述不同的对话框和多窗口设计组合使用,例如:
主窗口打开文件对话框选择文件。 使用自定义对话框进行数据输入或设置。 显示进度对话框进行长时间操作的进度显示。