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. How do you get access to widgets in a QDialog created in QtDesigner?
Forum Updated to NodeBB v4.3 + New Features

How do you get access to widgets in a QDialog created in QtDesigner?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 7.2k 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.
  • L Offline
    L Offline
    Little Red Wolf
    wrote on last edited by
    #1

    I'm showing a QDialog that I created in QtDesigner. How do I get access to the widgets that were added to it so that after the user closes it I can check its state?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      You don't do that directly because the widgets are private. If you need to extract information from the dialog, from outside the dialog class, then you need to provide public accessor functions in the dialog class.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Little Red Wolf
        wrote on last edited by
        #3

        How do you do it then? I created a dialog ui. What do I do with it? The examples are very vague and there seem to be no clear guidance of how to use a qtdesigner created Dialog ui (not a regular widget, but a dialog).

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

          You cannot rely on the designer to do everything, you still have to type some code. Either write a public accessor function as advised or go for signals and slots. The dialog itself is a type of widget, so it makes no difference, you can access UI elements created with the designer through the ui pointer.

          Writing accessor functions for private members is C++ 101

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CarlStenquist
            wrote on last edited by
            #5

            When calling the outside class function from MainWindow, you could pass a pointer to the widget .

            See my example (CarlStenquist) in
            How to access <widget> in ui placed on other ui:
            http://qt-project.org/forums/viewthread/21180

            P.S. I'm not really a "lab rat". Just an applications engineer.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              john_god
              wrote on last edited by
              #6

              Here's an example of how to set a "Hello World" in a lineEdit of a dialog called TestWidget, before showing it, and how to get the user input from that dialog after it's closed:

              testwidget.h
              @class TestWidget : public QDialog
              {
              Q_OBJECT

              public:
              explicit TestWidget(QWidget *parent = 0);
              ~TestWidget();

              //add this getter and setter
              void setText(const QString &text_);
              QString getText();
              

              private:
              Ui::TestWidget *ui;
              };@

              testwidget.cpp
              @void TestWidget::setText(const QString &text_)
              {
              ui->lineEdit->setText(text_);
              }

              QString TestWidget::getText()
              {
              return ui->lineEdit->text();
              }
              @

              Now calling the widget from MainWindow:
              @void MainWindow::on_actionTest_triggered()
              {
              TestWidget widget;

              widget.setText("Hello World !!!");
              
              if (widget.exec&#40;&#41; == QDialog::Accepted&#41;
              {
                  QString output = widget.getText(&#41;;
                  qDebug()<<output;
              }
              

              }
              @

              There you go :)

              @CarlStenquist: Please use the Code tag to format your text for better reading.

              Edit: Upps, I just notice this post is so damn old ...

              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