#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFontDialog>
void initFileMenu(MainWindow* mainWindow, QMenu* fileMenu);
void testMutableInLambda(QWidget* parent);
void testReturnInLambda();
void testOnButtonClick(MainWindow* mainWindow);
void showModeDialog(QWidget* parent);
void showNoneModeDialog(QWidget* parent);
void showMessageBox(QWidget* parent, int dialogType);
void showColorDialog(QWidget* parent);
void showFileDialog(QWidget* parent);
void showFontDialog(QWidget* parent);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
resize(600, 400);
initFileMenuItems();
initMenuBar();
initToolBar();
initStatusBar();
initDockWidget();
initCentralWidget();
}
void MainWindow::initCentralWidget() {
this->textEdit = new QTextEdit(this);
setCentralWidget(this->textEdit);
}
void MainWindow::initDockWidget() {
this->dockWidget = new QDockWidget("Float Content");
addDockWidget(Qt::BottomDockWidgetArea, this->dockWidget);
}
void MainWindow::initStatusBar() {
this->myStatusBar = statusBar();
setStatusBar(myStatusBar);
// 放入标签控件
QLabel* label = new QLabel("Tips");
myStatusBar->addWidget(label);
// 右侧持久标签
QLabel* rightLabel = new QLabel("Right Tips");
myStatusBar->addPermanentWidget(rightLabel);
}
void MainWindow::initToolBar() {
this->toolBar = new QToolBar(this);
addToolBar(Qt::TopToolBarArea, this->toolBar);
toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
toolBar->setFloatable(false);
toolBar->setMovable(false);
toolBar->addAction(this->fileMenuItems.at(0));
toolBar->addSeparator();
toolBar->addAction(this->fileMenuItems.at(1));
}
void MainWindow::initMenuBar() {
this->myMenuBar = menuBar();
this->setMenuBar(myMenuBar);
QMenu* fileMenu = myMenuBar->addMenu("File");
initFileMenu(this, fileMenu);
QMenu* editorMenu = myMenuBar->addMenu("Edit");
}
void MainWindow::initFileMenuItems() {
QAction* newAction = new QAction("New");
//使用QT资源: : + 前缀名 + 资源名
newAction->setIcon(QIcon(":/image/robot_hand.svg"));
this->fileMenuItems.append(newAction);
connect(newAction, &QAction::triggered, this, [=](){
// showModeDialog(this);
// showNoneModeDialog(this);
// showMessageBox(this, 4);
// showColorDialog(this);
// showFileDialog(this);
showFontDialog(this);
});
this->fileMenuItems.append(new QAction("Open"));
}
void showFontDialog(QWidget* parent) {
bool flag;
QFont font = QFontDialog::getFont(&flag, QFont("Times New Roman", 14));
QString res = QString("Family:%1, Size:%2, Bold:%3, Italic:%4").arg(font.family()).arg(font.pointSize()).arg(font.bold())
.arg(font.italic());
qDebug() << res;
}
void showFileDialog(QWidget* parent) {
QString filePath = QFileDialog::getOpenFileName(parent, "Sk Open File", "C:\\Users\\imt2047\\Desktop", "(*.png)");
qDebug() << filePath;
}
void showColorDialog(QWidget* parent) {
QColor color = QColorDialog::getColor(QColor(255, 0, 0));
QString str = QString("[%1, %2, %3]").arg(color.red()).arg(color.green()).arg(color.blue());
qDebug() << str;
}
void showMessageBox(QWidget* parent, int dialogType) {
switch (dialogType) {
case 1:
{
QMessageBox::critical(parent, "critial", "critial happened");
break;
}
case 2:
{
QMessageBox::information(parent, "information", "information happened");
break;
}
case 3:
{
QMessageBox::StandardButton result = QMessageBox::question(parent, "ques", "question happened", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (result == QMessageBox::Yes) {
qDebug() << "Yes";
} else {
qDebug() << "No";
}
break;
}
case 4:
{
QMessageBox::warning(parent, "warning", "warning happened");
break;
}
default:
QMessageBox::critical(parent, "critial", "critial happened");
break;
}
}
void showNoneModeDialog(QWidget* parent) {
QDialog* dialog = new QDialog(parent);
dialog->resize(200, 100);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
}
void showModeDialog(QWidget* parent) {
QDialog dialog(parent);
dialog.resize(200, 100);
dialog.exec();
}
void initFileMenu(MainWindow* mainWindow, QMenu* fileMenu) {
QList<QAction*> fileMenuItems = mainWindow->getFileMenuItems();
fileMenu->addAction(fileMenuItems.at(0));
fileMenu->addSeparator();
fileMenu->addAction(fileMenuItems.at(1));
}
void testConnect(MainWindow* mainWindow) {
Teacher* teacher = mainWindow->getTeacher();
Student* student = mainWindow->getStudent();
QPushButton* classOverButton = mainWindow->getClassOverButton();
teacher = new Teacher(mainWindow);
student = new Student(mainWindow);
void (Teacher::*hungryMethod)(QString) = &Teacher::hungry;
void (Student::*treatMethod)(QString) = &Student::treat;
mainWindow->connect(teacher, hungryMethod, student, treatMethod);
classOverButton = new QPushButton("class over", mainWindow);
// connect(classOverButton, &QPushButton::clicked, this, &MainWindow::classOver);
//信号连接信号
void (Teacher::*hungryMethodPure)(void) = &Teacher::hungry;
void (Student::*treatMethodPure)(void) = &Student::treat;
mainWindow->connect(teacher, hungryMethodPure, student, treatMethodPure);
mainWindow->connect(classOverButton, &QPushButton::clicked, teacher, hungryMethodPure);
}
/**
* @brief testOnButtonClick,利用lambda表达式实现槽函数功能
*
* @param mainWindow
*/
void testOnButtonClick(MainWindow* mainWindow) {
QPushButton* button = new QPushButton("Clock window", mainWindow);
button->move(100, 100);
mainWindow->connect(button, &QPushButton::clicked, mainWindow, [=](){
mainWindow->getStudent()->treat("Coco cola");
mainWindow->close();
});
}
void testReturnInLambda() {
int ret = []()->int{return 1000;}();
qDebug() << "ret = " << ret;
}
void testMutableInLambda(QWidget* parent) {
//mutable关键字表示可以修改值传递的变量,但是修改的是拷贝,并不是变量本身
QPushButton* myBtn = new QPushButton(parent);
QPushButton* myBtn2 = new QPushButton(parent);
myBtn2->move(100, 100);
int m = 10;
parent->connect(myBtn, &QPushButton::clicked, parent, [m]()mutable {m = 110; qDebug() << m;});
parent->connect(myBtn2, &QPushButton::clicked, parent, [=](){qDebug() << m;});
qDebug() << m;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::classOver() {
emit this->teacher->hungry("Noodles");
}
QList<QAction*> MainWindow::getFileMenuItems() {
return this->fileMenuItems;
}
QMenuBar* MainWindow::getMyMenuBar() {
return this->myMenuBar;
}
QPushButton* MainWindow::getClassOverButton() {
return this->classOverButton;
}
Teacher* MainWindow::getTeacher() {
return this->teacher;
}
Student* MainWindow::getStudent() {
return this->student;
}
QStatusBar *MainWindow::getMyStatusBar() const
{
return myStatusBar;
}
void MainWindow::setMyStatusBar(QStatusBar *value)
{
myStatusBar = value;
}
QDockWidget *MainWindow::getDockWidget() const
{
return dockWidget;
}
void MainWindow::setDockWidget(QDockWidget *value)
{
dockWidget = value;
}
没有合适的资源?快使用搜索试试~ 我知道了~
QT学习Demo例程1
共12个文件
cpp:4个
h:3个
user:1个
需积分: 0 0 下载量 169 浏览量
2023-11-02
21:05:17
上传
评论
收藏 8KB RAR 举报
温馨提示
QT学习Demo例程1QT学习Demo例程1QT学习Demo例程1
资源推荐
资源详情
资源评论
收起资源包目录
HelloWorld.rar (12个子文件)
HelloWorld
mainwindow.h 1KB
image
robot_hand.svg 2KB
mainwindow.cpp 8KB
teacher.h 267B
HelloWorld.pro.user 22KB
main.cpp 183B
mainwindow.ui 558B
HelloWorld.pro 1KB
res.qrc 104B
teacher.cpp 85B
student.h 282B
student.cpp 297B
共 12 条
- 1
资源评论
kaikai_sk
- 粉丝: 258
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功