欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

QT實(shí)現(xiàn)按鈕開(kāi)關(guān)Form窗體的效果的示例代碼

 更新時(shí)間:2023年07月13日 15:49:53   作者:羅伯特祥  
本文主要介紹了QT實(shí)現(xiàn)按鈕開(kāi)關(guān)Form窗體的效果的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

實(shí)現(xiàn)效果敘述如下: MainWindow中的按鈕實(shí)現(xiàn)Form窗體的開(kāi)關(guān),Form窗體的關(guān)閉按鈕禁用掉,只允許使用窗體按鈕進(jìn)行,且關(guān)閉MainWindow按鈕時(shí)Form窗體隨之關(guān)閉。

請(qǐng)?zhí)砑訄D片描述

注意: 要想實(shí)現(xiàn)關(guān)閉MainWindow按鈕時(shí)Form窗體隨之關(guān)閉,Form窗體的close()MainWindow的析構(gòu)函數(shù)中無(wú)法實(shí)現(xiàn),需要將其寫(xiě)入MainWindow的關(guān)閉事件中。

廢話不多說(shuō),直接上代碼:

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 *); // 重寫(xiě)MainWindow的關(guān)閉事件
private slots:
    void on_pushButton_clicked();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.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(); // 此處為了實(shí)現(xiàn)關(guān)閉MainWindow的同時(shí)也關(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實(shí)現(xiàn)按鈕開(kāi)關(guān)Form窗體的效果的示例代碼的文章就介紹到這了,更多相關(guān)QT按鈕開(kāi)關(guān)Form窗體內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論