Update mainWindow when click the button.



  • My code
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)

    {
    createbutton(global variableA from OnClicked); //function to select and create button depends on variableA
    }

    void MainWindow::OnClicked()
    {
    // when clicked change value of global variableA
    }

    /*My program actually look like this
    for some reason I have to recall function in main.I wonder if there is any function to recall main.Plz help me.
    Thank you for all answer.



  • I do not quite understand what you want, but here's a small example:
    @
    // main.cpp

    #include <QApplication>
    #include <QMainWindow>
    #include <QPushButton>
    #include <QMessageBox>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0) :
    QMainWindow(parent)
    {
    QPushButton *button = new QPushButton("Click me");
    connect(button, SIGNAL(clicked()), this, SLOT(OnClicked()));
    setCentralWidget(button);
    }

    private slots:
    void OnClicked()
    {
    // Do something
    QMessageBox::information(this, "Handler", "From MainWindow::OnClicked()");
    }
    };

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    MainWindow w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }

    #include "main.moc"
    @



  • what if I want to change position of the button everytimes I click.
    We have to create the button in MainWindow right?
    How can I send the position value from click event to Main that what I want.

    I'm sorry I'm just a beginner in using English and qt also.
    My words is hard to understand.
    Thank you very much Konstantin Podsvirov and all of the answer



  • I'm currently using repaint() in QPainter.
    I'm just looking for the same issues but work for MainWindow.



  • Try to study the example:
    @
    // main.cpp

    #include <QApplication>
    #include <QMainWindow>
    #include <QPushButton>
    #include <QMessageBox>
    #include <QMouseEvent>

    class ButtonWidget: public QWidget
    {
    public:
    ButtonWidget()
    {
    button = new QPushButton("Click me", this);
    }

    QPushButton *button;
    

    protected:
    void mousePressEvent(QMouseEvent *event)
    {
    button->setGeometry(QRect(event->pos(), button->size()));
    }
    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0) :
    QMainWindow(parent)
    {
    ButtonWidget *widget = new ButtonWidget;
    connect(widget->button, SIGNAL(clicked()), this, SLOT(OnClicked()));
    setCentralWidget(widget);
    }

    private slots:
    void OnClicked()
    {
    // Do something
    QMessageBox::information(this, "Handler", "From MainWindow::OnClicked()");
    }
    };

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    MainWindow w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }

    #include "main.moc"
    @


Log in to reply
 

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