Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] refresh QMainWindow from another Dialog
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] refresh QMainWindow from another Dialog

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 10.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adriien
    wrote on last edited by
    #1

    Hi everybody,

    I want to add an Item in a QComboBox which is in my topWidget from another QDialog. The topWidget is in the QMainWindow.

    QMainWindow
    @
    #include "testcombobox.h"
    #include "widtop.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    widtop *widt = new widtop();
    ui->topLayout->addWidget(widt);
    }

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

    QWidget
    @
    #include "widtop.h"
    #include "ui_widtop.h"

    widtop::widtop(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::widtop)
    {
    ui->setupUi(this);
    ui->comboBox->addItem("Item1");
    ui->comboBox->addItem("Item2");
    }

    widtop::~widtop()
    {
    delete ui;
    }

    void widtop::addItemFunction()
    {
    ui->comboBox->addItem("You did it !");
    }
    @

    QDialog
    @
    testcombobox::testcombobox(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::testcombobox)
    {
    ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked(), .... , .... )
    }

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

    If this problem can be solved with a signal, I don't know what to write in it, but maybe there is another solution ?

    Thank you in advance, Adrien.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      puksec
      wrote on last edited by
      #2

      Well

      @connect(ui->pushButton, SIGNAL(clicked(), widtop_instance , SLOT(addItemFunction()) )@

      would be an obvious , if that is what you mean.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adriien
        wrote on last edited by
        #3

        Hum, that was what I mean but, it didn't solve my problem.

        Like I said, "I want to add an Item in a QComboBox which is in my topWidget from another QDialog. The topWidget is in the QMainWindow.".

        So with this connection, I can call the function to add an Item but it seems that the QMainWindow does not refresh or something like that.

        If I put the same addItemFunction in the QMainWindow and trigger it with a QButton, it works but I don't want to push any button to refresh.

        How to "refresh" my QMainWindow or forcing to reload the widget ?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          puksec
          wrote on last edited by
          #4

          You can use QApplication::processEvents() to force execution of all pending events in applications event loop, but I don't think that that's your problem. Try this:
          @testcombobox::testcombobox(QWidget *parent, widtop *wt) :
          QDialog(parent),
          ui(new Ui::testcombobox)
          {
          ui->setupUi(this);
          connect(ui->pushButton, SIGNAL(clicked(), wt , SLOT(addItemFunction()) )
          }@

          where you would pass an pointer to instance of widtop that you want to be updated. If this is not working, try making your dialogs non modal (if they are).

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adriien
            wrote on last edited by
            #5

            I just tried with non modal window and QApplication::processEvents() and nothing happens.

            The comboBox stay only with "Item1" and "Item2" previously added.

            Maybe there is no way to modify a QMainWindow from another dialog ? In this case I will simply create a refresh button :/

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              There's one thing that you don't show, it's where you do call your dialog and how you use it

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adriien
                wrote on last edited by
                #7

                Hello,

                Okay, I will explain again very clearly. I have just created a new project to test and test again.

                QMainWindow
                @
                #include "mainwindow.h"
                #include "ui_mainwindow.h"

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);
                widtop *w = new widtop();
                ui->layout->addWidget(w);
                }

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

                QWidget
                @
                #include "widtop.h"
                #include "ui_widtop.h"
                #include "dialog.h"

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

                widtop::~widtop()
                {
                delete ui;
                }

                void widtop::addItemFunction()
                {
                ui->comboBox->addItem("You did it");
                }

                void widtop::on_pushButton_clicked()
                {
                dialog *t = new dialog();
                t->setModal(false);
                t->exec();
                }
                @

                QDialog
                @
                #include "dialog.h"
                #include "ui_dialog.h"
                #include "widtop.h"

                dialog::dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::dialog)
                {
                ui->setupUi(this);
                widtop *w = new widtop();
                connect(ui->pushButton, SIGNAL(clicked()), w, SLOT(addItemFunction()));
                //QApplication::processEvents();
                }

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

                And a little screenshot: http://upz.fr/c7Ta6.png

                If you have some ideas to move a function from a file to another to make it works, I take it.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Ok, I see the problem. You are not doing what you think you are doing. You update the widtop(that you don't show and you leak) in your dialog which is not the widtop from your MainWindow.
                  Try with this:
                  @
                  dialog.h
                  {
                  // your stuff

                  signals:
                  void buttonClicked(); // << bad naming, only for test
                  }

                  dialog.cpp
                  dialog::dialog(QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::dialog)
                  {
                  ui->setupUi(this);
                  connect(ui->pushButton, SIGNAL(clicked()), SIGNAL(buttonClicked()));
                  }

                  widtop.cpp
                  void widtop::on_pushButton_clicked()
                  {
                  // you were creating a memory leak
                  // when using new, always have a corresponding delete
                  // UNLESS you're using Qt's parent/child system
                  dialog t;
                  t.setModal(false);
                  connect(&t, SIGNAL(buttonClicked()), SLOT(addItemFunction()));
                  t.exec();
                  }
                  @

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    adriien
                    wrote on last edited by
                    #9

                    Thank you very much, your solution works perfectly.

                    I think I should read more documentation about signals because it's not obvious at all for me to connect a SIGNAL to a SIGNAL, that's pretty strange.

                    But problem solved, many thanks !

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You're welcome !

                      Indeed, and there's plenty of example that are useful to learn the basics. Not really strange, it's signal forwarding. You can use it to avoid tight coupling, like in this case, you would either need to have your dialog class know widtop or give widtop access to your dialog button to make the connection. Both of which are not clean design.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved