mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MyWidget *ui;
};
#endif // MYWIDGET_H
main.cpp
#include "mywidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
#include <QDebug>
#include <QColorDialog>
#include <QFileDialog>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
}
MyWidget::~MyWidget()
{
delete ui;
}
void MyWidget::on_pushButton_clicked()
{
QColorDialog dialog(Qt::red, this); // 创建对象
dialog.setOption(QColorDialog::ShowAlphaChannel); // 显示 alpha 选项
dialog.exec(); // 以模态方式运行对话框
QColor color = dialog.currentColor(); // 获取当前颜色
qDebug() << "color:" << color; // 输出颜色信息
}
void MyWidget::on_pushButton_2_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("文件对话框"), "D:", tr("图片文件( * png * jpg);;文本文件( * txt)"));
qDebug() << "fileName:" << fileName;
}