How do you get access to widgets in a QDialog created in QtDesigner?
-
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?
-
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).
-
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
-
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 -
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_OBJECTpublic:
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() == QDialog::Accepted) { QString output = widget.getText(); 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 ...