QT實現(xiàn)按鈕開關(guān)Form窗體的效果的示例代碼
實現(xiàn)效果敘述如下: MainWindow中的按鈕實現(xiàn)Form窗體的開關(guān),Form窗體的關(guān)閉按鈕禁用掉,只允許使用窗體按鈕進行,且關(guān)閉MainWindow按鈕時Form窗體隨之關(guān)閉。

注意: 要想實現(xiàn)關(guān)閉MainWindow按鈕時Form窗體隨之關(guān)閉,Form窗體的close()在MainWindow的析構(gòu)函數(shù)中無法實現(xiàn),需要將其寫入MainWindow的關(guān)閉事件中。
廢話不多說,直接上代碼:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}form.c
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
setWindowFlags(Qt::CustomizeWindowHint |
Qt::WindowMinimizeButtonHint |
Qt::WindowMaximizeButtonHint); // 禁用Form窗體的關(guān)閉按鈕
}
Form::~Form()
{
delete ui;
}mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void closeEvent(QCloseEvent *); // 重寫MainWindow的關(guān)閉事件
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QDebug>
static bool newWinFlag = false;
Form *configWindow = NULL;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
configWindow = new Form;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *e)
{
configWindow->close(); // 此處為了實現(xiàn)關(guān)閉MainWindow的同時也關(guān)閉Form窗體
}
void MainWindow::on_pushButton_clicked()
{
qDebug() << newWinFlag;
newWinFlag = !newWinFlag;
if(newWinFlag == true)
{
configWindow->show();
return;
}
else
{
configWindow->close();
return;
}
}form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
private:
Ui::Form *ui;
};
#endif // FORM_H到此這篇關(guān)于QT實現(xiàn)按鈕開關(guān)Form窗體的效果的示例代碼的文章就介紹到這了,更多相關(guān)QT按鈕開關(guān)Form窗體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
怎么實現(xiàn)類的成員函數(shù)作為回調(diào)函數(shù)
不使用成員函數(shù),為了訪問類的成員變量,可以使用友元操作符(friend),在C++中將該函數(shù)說明為類的友元即可2013-10-10
C++實現(xiàn)LeetCode(121.買賣股票的最佳時間)
這篇文章主要介紹了C++實現(xiàn)LeetCode(121.買賣股票的最佳時間),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
C++設(shè)計模式之工廠方法模式的實現(xiàn)及優(yōu)缺點
工廠方法模式是一個創(chuàng)建型設(shè)計模式,通過定義一個創(chuàng)建對象的接口,讓其子類決定實例化哪一個工廠類,這篇文章主要給大家介紹了關(guān)于C++設(shè)計模式之工廠方法模式的實現(xiàn)及優(yōu)缺點,需要的朋友可以參考下2021-06-06
C++快速調(diào)用DeepSeek API的完整指南
最近,DeepSeek的API引起了我的興趣,它提供了強大的對話生成能力,可以用于多種應(yīng)用場景,雖然DeepSeek官方提供了詳細的API文檔,但遺憾的是,目前沒有專門針對C++的調(diào)用示例,所以,本文給大家實現(xiàn)一個C++版本的調(diào)用示例,需要的朋友可以參考下2025-03-03

