L
L
l2p2014-09-06 11:33:33
Qt
l2p, 2014-09-06 11:33:33

How to use the interface (ui) in the generated header file?

I welcome everyone!
I created the add.h file, where I placed the class I needed. How do I call the ui-> methods on it?
In .cpp I wrote #include "add.h"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EXL, 2014-09-06
@l2p

If you need dynamic dialogs, then you can do this:

QUiLoader uiLoader;
QFile file("sortdialog.ui");
QWidget *sortDialog = uiLoader.load(&file);
if (sortDialog) {
    ....
}

Source .
But most likely you just need to create a form with a class. To do this, in Qt Creator, select "File" -> "New File or Project" -> "Qt" -> "Qt Designer Form Class".
If you wish, you can copy the contents from the old one into the new *.ui form file (or even overwrite it with your old *.ui). Everything will work.
And if you need to commit changes to the form from an external class, you can always use the pointer:
TestClass.h:
#ifndef TESTCLASS_H
#define TESTCLASS_H

#include "ui_MainWindow.h"

class TestClass
{
public:
    TestClass();
    void changeTextOnForm(Ui_MainWindow *mainWindow);
};

#endif // TESTCLASS_H

TestClass.cpp:
#include "TestClass.h"

TestClass::TestClass()
{
}

void TestClass::changeTextOnForm(Ui_MainWindow *mainWindow)
{
    mainWindow->label_2->setText(QString("Text from ") + Q_FUNC_INFO);
    mainWindow->textBrowser->append(mainWindow->label_2->text());
}

MainWindow.cpp:
...
void MainWindow::on_pushButton_clicked()
{
    TestClass testClass;
    testClass.changeTextOnForm(this->ui);
}
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question