SOLVED: Hide frame when button is clicked()



  • Hello,

    I created a MainWindow and placed a QFrame on it. Then placed a button on top of the frame. I connected the 2 objects so that the frame is supposed to hide when the button is clicked but I get no response. Below is the code for mainwindow.cpp, the rest of the files are the default for a MainWindow project.

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFrame>
    #include <QPushButton>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QFrame *frame = new QFrame(this);
    setCentralWidget(frame);
    
    QPushButton *button = new QPushButton(this);
    button->setGeometry(20,20,50,20);
    button->setText("Hide Frame");
    
    connect(button, SIGNAL(clicked()), frame, SLOT(hide()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    Thanks in Advance


  • Moderators

    @
    QPushButton *button = new QPushButton(frame);
    @

    Otherwise only the frame will hide. Consider using layouts, too. Fixed geometry is a hell to maintain.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.