Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] manipulating qmainwindow widget through qdialog
Qt 6.12 LTS is out! See what's new in the release blog

[SOLVED] manipulating qmainwindow widget through qdialog

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 13.4k 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.
  • X Offline
    X Offline
    xeroblast
    wrote on last edited by
    #1

    hi, i have a qtablewidget in my mainwindow and when a dialog opens, there is a bunch of qlinedit and when i click the button add in the qdialog, the data in the qlinedits must be inserted in the qtablewidget in the mainwindow without closing the qdialog coz i have to insert another data in the qtablewidget but the problem is that the data is not reflected in the qtablewidget.

    mainwindow has a public slot for the qdialog to call. it run bcoz i tried to put a qmessagebox in each line but the qtablewidget setItem is not working.

    help what is wrong with it?

    this is my main window contructor :
    @
    MainWindow::MainWindow( QWidget * parent, Qt::WFlags f) : QMainWindow(parent, f)
    { }
    @
    this is my dialog child contructor :
    @
    DialogChild::DialogChild( QWidget * child ) : QDialog(child)
    { }
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Could you give some more code, because of this, I don't see the problem. More example of the code could help to find the problem.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        First of all, write your questions in proper English.

        Second, I don't see anything wrong in your design. You said you have a slot in your MainWindow, with a signature like
        @
        appendRow(const QList<Item> &);
        @

        Therefore, all you need is

        declare a corresponding signal (like, rowAdded()) in the Dialog

        connect the Dialog signal to the MainWindow slot

        create a button in the dialog (you may want to make this the default button) and connect its clicked() signal to another Dialog slot, in which you fetch the data from the lineedits and emit the rowAdded() signal.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xeroblast
          wrote on last edited by
          #4

          this is my code to add items in qtablewidget.

          @
          void MainWindow::setData(QString text)
          int i = tableWidget->rowCount();
          tablewidget->insertRow(i);
          QTableWidgetItem *txt = new QTableWidgetItem(text);
          tableWidget->setItem(i, 0, txt);
          @

          mainwindow.h
          @
          public slots:
          void setData(QString);
          private slots:
          void openDialog();
          @

          dialog.cpp
          @
          void Dialog::clickAdd()
          {
          MainWindow form;
          form.setData(lineEdit->text());
          lineEdit->setData(""); // Empty the lineEdit coz I need to insert another data
          }
          @

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            You instantiate a MainWindow in clickAdd(), add some data, and when the method ends, the form goes out of scope and is destroyed. Not a Qt problem, but some misunderstanding of C/C++ scopes for variables and objects.

            peppe already suggested a proper way to achieve this task.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              You need a pointer to the main window in your dialog. Perhaps it's the parent? And you should connect the signal of the dialog with the slot of the main window.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xeroblast
                wrote on last edited by
                #7

                sorry for my late reply... my internet has a little problem last night..

                bq. create a button in the dialog (you may want to make this the default button) and connect its clicked() signal to another Dialog slot, in which you fetch the data from the lineedits and emit the rowAdded() signal.

                i dont understand the another dialog part..
                as i understand it, dialog, inserts data to the anotherdialog then after dialog is close, mainwindow gets data from another dialog...

                and how do i make a pointer to the mainwindow or the parent?

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  xeroblast
                  wrote on last edited by
                  #8

                  i found this : http://www.qtcentre.org/threads/24755-How-to-add-data-into-the-QTableWidget
                  but i get the following error : src/dialog.cpp:5: error: class 'Dialog' does not have any field named 'm_table'

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    xeroblast
                    wrote on last edited by
                    #9

                    solved it on my own :

                    mainwindow.cpp
                    @
                    MainWindow::openDialog()
                    {
                    Dialog d(this, tableWidget);
                    d.exec();
                    }
                    @
                    dialog.cpp
                    @
                    QTableWidget *p_table;
                    Dialog::Dialog(QWidget * parent , QTableWidget * table) : QDialog(parent)
                    {
                    setupUi(this);
                    p_table = table;
                    }
                    @

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      Hi,

                      and that's exaclty how to get the pointer to the parent:

                      @
                      QTableWidget *p_table;
                      Dialog::Dialog(QWidget * parent , QTableWidget * table) : QDialog(parent)
                      {
                      setupUi(this);
                      p_table = table;
                      }
                      @

                      The parent pointer. That could be cast to the MainWindow class. Or the main window could make a connect to a custom signal:

                      @
                      MainWindow::openDialog()
                      {
                      Dialog d(this, tableWidget);
                      connect(&d, SIGNAL(MyCustomSignal(...)), this, SLOT(MySlotToAddSomeStuff(...)));
                      d.exec();
                      }@

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0
                      • X Offline
                        X Offline
                        xeroblast
                        wrote on last edited by
                        #11

                        really thanx...

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          Using signals and slots would be the better way, IMHO. And mor Qt'ish, too.

                          This also has the advantage, that your dialog needs not to know anything about your list widget. In case you change your mind and replace the list widget by another widget, you only have to change the main windows implementation, but not the dialogs, as the signal remains the same.

                          Basically the code is like this:

                          dialog.h
                          @
                          class Dialog: public QDialog {
                          public:
                          Dialog(QWidget *parent);
                          // ... more stuff

                          protected slots:
                          void saveButtonClicked();

                          signals:
                          void requestNewEntry(const QString &name, int amount, QDate date);
                          }
                          @

                          dialog.cpp
                          @
                          Dialog::Dialog(QWidget *parent) : QDialog(parent)
                          {
                          // do your usual stuff here
                          connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
                          }

                          void Dialog::saveButtonClicked()
                          {
                          QString name = lineEdit->text();
                          int amount = intSpinBoxl->value();
                          QDate date = dateEdit->date();
                          emit requestNewEntry(name, amount, date);
                          }
                          @

                          mainwindow.h
                          @
                          class MainWindow: public QMainWindow {
                          public:
                          // constructors etc.

                          public slots:
                          void addEntry(const QString &name, int amount, QDate date);

                          protected slots:
                          void showDialog();
                          }
                          @

                          mainwindow.cpp
                          @
                          void MainWindow::openDialog()
                          {
                          Dialog *d = new Dialog(this);
                          connect(d, SIGNAL(requestNewEntry(QString, int, QDate)), this, SLOT(addEntry(QString, int, QDate)));
                          d->show()
                          }

                          void MainWindow::addEntry(const QString &name, int amount, QDate date)
                          {
                          // create the items here depending on the value
                          QListItem *i1 = new QListItem(val1);
                          table->setItem(row, col, i1)
                          }
                          @

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dangelog
                            wrote on last edited by
                            #13

                            I like the fact that I write how the solution should be, and someone else writes the corresponding code :-D

                            Software Engineer
                            KDAB (UK) Ltd., a KDAB Group company

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #14

                              [quote author="peppe" date="1292491829"]I like the fact that I write how the solution should be, and someone else writes the corresponding code :-D[/quote]

                              Shouldn't that be always the case in a our ideal world? ;-)

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                giesbert
                                wrote on last edited by
                                #15

                                Can I borrow you for some time? I also need people, writing the correct code for my problems :-)))

                                Nokia Certified Qt Specialist.
                                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                1 Reply Last reply
                                0
                                • ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #16

                                  You might be lucky, I'm a rent-that-coding-guy :-)

                                  1 Reply Last reply
                                  0
                                  • X Offline
                                    X Offline
                                    xeroblast
                                    wrote on last edited by
                                    #17

                                    i like this forum... there's a lot of help... just like the codeigniter forum... :D

                                    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
                                    • Unsolved