Accessing button in QWidget of MainWindow



  • I am new to Qt and sorry if this sounds like a noob question. I have a main Window with a Push button that opens up a dialog with 5 other push buttons. I need an executable file to run every time I click one of the 5 push buttons. (I will be using QProcess). I need to know how to access the button and detecting if it was clicked in the second window. Please help. I am using Qt4.8.6 with Visual Studio 2010.

    This is my code

    examp1.h

    @#ifndef EXAMP1_H
    #define EXAMP1_H

    #include <QtGui/QMainWindow>
    #include "ui_examp1.h"

    class examp1 : public QMainWindow
    {
    Q_OBJECT

    public:
    examp1(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~examp1();

    private:
    Ui::examp1Class ui;

    public slots:
    void on_pushButton_clicked();
    };

    #endif // EXAMP1_H@

    examp1.cpp

    @#include "examp1.h"
    #include<QtGui\qfiledialog.h>
    #include<QtGui\qgridlayout.h>

    examp1::examp1(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    ui.setupUi(this);
    }

    void examp1::on_pushButton_clicked() {

    QDialog d;
    QPushButton *button11 = new QPushButton("Button1");
    QPushButton *button12 = new QPushButton("Button2");
    QPushButton *button13 = new QPushButton("Button3");
    QPushButton *button14 = new QPushButton("Button4");
    QPushButton *button15 = new QPushButton("Button5");
    QLabel *label = new QLabel;
    label->setText("Choose your activity");

    QGridLayout *layout1 = new QGridLayout;
    

    layout1->addWidget(label);
    layout1->addWidget(button11, 0, 0);
    layout1->addWidget(button12, 0, 1);
    layout1->addWidget(button13, 1, 0, 1, 2);
    layout1->addWidget(button14, 2, 0);
    layout1->addWidget(button15, 2, 1);

    button11->setStyleSheet("background-color: rgb(255, 255, 255);");
    button12->setStyleSheet("background-color: rgb(255, 255, 255);");
    button13->setStyleSheet("background-color: rgb(255, 255, 255);");
    button14->setStyleSheet("background-color: rgb(255, 255, 255);");
    button15->setStyleSheet("background-color: rgb(255, 255, 255);");

    d.setLayout(layout1);
    d.showMaximized();
    d.setMinimumSize(500,500);
    d.setStyleSheet("background-color: rgb(48, 96, 255);");
    d.exec();
    }

    examp1::~examp1()
    {

    }@

    main.cpp

    @#include "examp1.h"
    #include <QtGui/QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    examp1 w;
    w.show();
    return a.exec();
    }
    @



  • connect your QPushButton Signal 'clicked()' to a slot.

    1.) Define a slot in your class header in which you start your QProcess()
    2.) Probably better to move your QPushButton declaration to class member also or create a QButtonGroup as a classmember holding the buttons.
    3.) connect the buttons 'clicked()' signal to the slot.
    4.) be happy



  • I have only one class examp1. I am not able to view the dialog in the designer. So how do I connect the signal to the slot?



  • Request you to read the signals and slots in Qt Assistant. Look at how connect(...) works.

    Also look at clicked() signal of QPushButton as well.

    Anyway.

    @Define the slots in in you class like the following.

    public slots:
    void buttonClicked()

    void exam1::buttonClicked() {

    qDebug() << "I am here " << endl;
    }

    Add the following line after button creation for every buttons.

    connect(buton1,SIGNAL(clicked()),this,SLOT(buttonClicked())@



  • But aren't you defining this for the mainWindow? I want to access the ones in the dialog box



  • According to your explanation idea is that whenever button is clicked you need some notification. Is that correct ? In that case this works. Did you try this ? Also variable dialog d is declared as stack variable. It will be lost once function closes.



  • I apologize for not being clear. I open my dialog by clicking on the push Button in the main window. This opens up a window with 5 other push button, each of while consists a task I want to perform. Is there any way I can access these 5 button that are present in the DIALOG? Or is there any other way to do this?



  • You can access those buttons. My question is why do you want access them through dialog ? Please note that Dialog is not written by you. You don't have much control. If you tell me why you want to access, we can provide you better solution.



  • I want to access it so that I can run an executable file when the button is clicked. The structure of my problem is as follows

    1. Main window with a start and end button.
    • When the start button is clicked, it should open a window with 5 push buttons
    • When end is clicked, it should invoke a window that asks if I am sure I want to quit(with yes or no options)
    1. When the push button is clicked in the second window, I should run a .exe file(probably using QProcess, I am guessing?)

    This is why I would like to access the buttons in the second window. Hope I was clear.



  • For this you don't go inside the dialog. Button references are already present in the MainWindow class. Connect these buttons clicked signal with slots inside the mainwinodw. Inside the slots, create QProcess and start the process. Hope this helps.


Log in to reply
 

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