
前言:
- Qt是C++一个开发框架,具有跨平台特性。
- 这篇是作者大二学习的时候做的笔记,有可能有错误,请各位批评指正。
- 这篇记录Qt的QWidget、QDialog。
- 欢迎大家收藏 + 关注,作者将会持续更新。
文章目录
QWidget
QWidget类是所有可视控件的基类,控件是用户界面的最小元素,用于接受各种事件(如:鼠标、键盘等)并且绘制出来给用户观看。
如果控件没有父控件,则称之为窗口。
-
窗口大小、移动、几何大小、隐藏、关闭、最小化、判断是否关闭等
c++//第一大部分:标题、图标、资源文件 //标题 setWindowTitle("myWidget"); //图标 setWindowIcon(QIcon(":/OIP-C (8).jpg")); //1、添加资源文件 2、cmake包含两个东西 //当然,相对和绝对路劲也行 //第二部分:窗口位置、大小、移动 //获取高度和宽度:1、窗口 2、几何大小 qint8 h = height(); qint8 w = width(); auto gh = geometry(); qDebug() << gh; qDebug() << gh.topLeft() << " " << gh.bottomRight(); qDebug() << gh.height() << " " << gh.width(); move(300, 200); //remove移动文件 //设置窗口大小 窗口、几何 resize(640, 480); setGeometry(400, 200, 500, 400); //设置固定窗口、固定大小 //setFixedSize(640, 480); setFixedWidth(640); setFixedHeight(480); //设置最小、最大大小 setMinimumHeight(100); setMinimumWidth(100);最大化、最小化、隐藏等
c++class _Widget : public QWidget { public: _Widget(QWidget* parent = nullptr) :QWidget(parent), m_max(new QPushButton("最大化", this)), m_min(new QPushButton("最小化", this)), m_hide(new QPushButton("隐藏",this)), m_normal(new QPushButton("正常化",this)), m_hideShow(new QPushButton("显示",this)) { resize(640, 480); m_max->move(100, 100); m_min->move(200, 100); m_normal->move(300, 100); m_hideShow->move(100, 0); connect(m_max, &QPushButton::clicked, [=]() { //this->showFullScreen(); //不含标题栏这些,内容全屏 this->showMaximized(); }); connect(m_min, &QPushButton::clicked, [this]() { this->showMinimized(); }); connect(m_normal, &QPushButton::clicked, [this]() { showNormal(); }); text_hide(); } void text_hide() { QDialog* dialog = new QDialog(this); dialog->show(); connect(m_hide, &QPushButton::clicked, [=]() { dialog->hide(); }); connect(m_hideShow, &QPushButton::clicked, [=]() { dialog->showNormal(); }); } private: QPushButton* m_max; QPushButton* m_min; QPushButton* m_normal; QPushButton* m_hide; QPushButton* m_hideShow; }; //注意:close不会关闭 需要设置属性 setAttribute(QT::QA_DeleteClose)
QDialog
对话框窗口是一个顶级窗口(没有指定父对象就是模态窗口),主要用于短期任务和与用户的简短通信。
对话框有两种模式:模态对话框、非模态对话框。
模态对话框
模态对话框是用于向用户请求文件名或用于设置应用程序首选项的对话框通常是模态的。对话框可以是应用程序模式 (默认)或窗口模式。
当打开应用程序模态对话框时,用户必须完成与对话框的交互并关闭它,然后才能访问应用程序中的任何其他窗口。
非模态对话框
非模态对话框是在同一应用程序中独立于其他窗口运行的对话框 。文字处理程序中的查找和替换对话框通常是非模态的,允许用户与应用程序的主窗口和对话框进行交互。
非模态对话框使用show()显示,它会立即将控制权返回给调用者。
- show
- open
- exec() 。
默认按钮
对话框的默认按钮是用户按Enter(返回)键时按下的按钮 。此按钮用于表示用户接受对话框的设置并希望关闭对话框。使用QPushButton::setDefault(), QPushButton::isDefault()和QPushButton::autoDefault()来设置和控制对话框的默认按钮。
ESC键
如果在对话框中按Esc键,QDialog::reject()将被调用。这将导致窗口关闭:关闭事件不能被忽略。
- reject
- accept
c++
//模态与非模态
void test_dialog()
{
connect(m_btn1, &QPushButton::clicked, [this]() {
QDialog* dialog1 = new QDialog(this);
dialog1->exec(); //注销dialog才会有
qDebug() << "exec dialog";
});
connect(m_btn2, &QPushButton::clicked, [this]() {
QDialog* dialog2 = new QDialog(this);
dialog2->open(); //半模态
qDebug() << "open dialog";
});
connect(m_btn3, &QPushButton::clicked, [this]() {
QDialog* dialog3 = new QDialog(this);
dialog3->show(); //非模态
qDebug() << "show dialog";
});
}
///--------------------------------------------------------------------------
//接受和取消
class Dialog : public QDialog
{
public:
Dialog(QDialog* parent = nullptr)
:QDialog(parent),
m_yesbtn(new QPushButton("确定",this)),
m_cancel(new QPushButton("取消", this))
{
resize(640, 480);
m_yesbtn->move(100, 100);
m_cancel->move(200, 100);
//ecs键 默认退出 可设置默认键
//m_cancel->setDefault(true);
//accepted and rejected 和 用 done 做接受和拒绝
connect(m_yesbtn, &QPushButton::clicked, [=]() {
#if 0
accept(); //注意 accepted() 是信号
#else
done(QDialog::Accepted);
#endif
qDebug() << "accept";
});
connect(m_cancel, &QPushButton::clicked, [=]() {
#if 0
reject(); //注意 rejected() 是信号
#else
done(QDialog::Rejected);
#endif
qDebug() << "reject";
});
}
public:
QPushButton* m_yesbtn;
QPushButton* m_cancel;
};