Qt案例学习-01-mainwindow点击弹出文本对话框

在这里插入图片描述

  • 新建mainwindow工程
  • 打开mainwindow.ui,点击编辑页面的左上角Type Here,重命名为File,摁enter键后,鼠标点击其所属的Type Here,然后重命名为New WIndow,摁 enter 键后,主界面下方出现了一行新内容
  • 在这里插入图片描述
  • 右键actionNew_Window,选择Go to slot,这时mainwindow.cpp里会自动新建一个函数on_action_WIndow_triggered(),后续会在里面定义新建的对话框的相关操作。
  • 返回edit页面,在Forms文件夹,右键Add new..., 依次选择Qt-Qt Designer Form- ->choose->Dialog without buttons->next->next…;将自己添加的dialog命名为MyDialog;
  • 点击mydialog.ui, 添加一个plain text edit
  • 打开mainwindow.h
1
2
3
4
//添加如下代码
#include "mydialog.h"
//scroll down untill you see "private:"
MyDialog *mDialog;
  • 打开mainwindow.cpp,添加如下代码:
1
2
3
4
5
#include "mydialog.h"

// add codes in the on_action_WIndow_triggered() function
mDialog = new MyDialog(this);
mDialog->show();
  • 保存,运行