Multiple UI files for Qt mobile app - c++

I have multpile ui files .
I want to navigate througth them according to pushbutton clicks.
How can I control this in the code ?
how can i do that?

Each ui file generally has cpp class assotiated with it.
Just create it and show.
class myGuiClass: public QWidget{...}; // class assotiated with ui
void SomeOtherClass::onSomePushButtonClicked()
{
myGuiClass* wnd= new myGuiClass(this);
wnd->show();
}

Related

QT QTextEdit setText crashes

I've created a qt widgets application. Using the design mode I've created a QTextEdit and indicated that in the header file:
...
QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QTextEdit;
QT_END_NAMESPACE
...
private:
Ui::MainWindow *ui;
QTextEdit *textEdit_2;
};
There is also a slot which is triggered by pushing a button. What it has to do is to insert some text into textEdit_2 after the button is pushed, still the program crashes.
In mainwindow.cpp:
void MainWindow::on_action_4_triggered()
{
textEdit_2->setText("text");
}
I've also tried
textEdit_2->setText(QString("text"));
which anyway doesn't work. What's the problem?
textEdit_2->setText("text");
The problem is that you are trying to ignore the actual text widget created in QtDesigner and invent another as a class member. This is not going to fly as you seem to want it.
In order to reuse the text widget from the UI that you created with the graphical tool, you would need to reuse the ui object as follows:
ui->textEdit_2->setText("text");
Please also note that you do not need to construct QString explicitly like this:
textEdit_2->setText(QString("text"));
This will be all automatic for you.

QWidget - Not sure where to start

I'm doing a college project in QT with C++. I basically have it all done, but I kind of want to end of a flourish! At the endgame, you get to the last room and press the control panel and a keypad appears (which is a .ui layout created as a QWidget) - the access code is a randomly generated 4 digit number in an earlier room.
Anyways, I want to pop up the QWidget with the keypad, get the user to press 4 buttons - each button would return a QString - and then press the confirm button. If it matches, game ends. If not, returned to room.
I just have no idea how to call the widget! The API haven't really help as I don't see anyway to assign a .ui form to the QWidget object.
The .ui file is a resource file. If the setup you have does not do this for you automatically, then you must use the uic tool to convert the .ui file to c++ source code.
foo.ui -> ui_foo.h
This header contains a class that creates the widgets and has members to acces each of the members once they have been created.
class Ui_Foo {
setupUi(QWidget *) { ...
}
}
namespace Ui {
class Foo: public Ui_Foo {};
} // namespace Ui
An instance of Ui::Foo is placed in your FooWidget
// FooWidget.h
//
class FooWidget
: public QWidget {
FooWidget(QWidget *);
Ui::Foo mUi;
}
and its setupUi is called in the constructor of your FooWidget
// FooWidget.cpp
//
FooWidget::FooWidget(QWidget *parent)
: QWidget(parent)
{
mUi.setupUi(this);
}
The .ui form is a widget. Just call show() on it.

Qt show modal dialog (.ui) on menu item click

I want to make a simple 'About' modal dialog, called from Help->About application menu. I've created a modal dialog window with QT Creator (.ui file).
What code should be in menu 'About' slot?
Now I have this code, but it shows up a new modal dialog (not based on my about.ui):
void MainWindow::on_actionAbout_triggered()
{
about = new QDialog(0,0);
about->show();
}
Thanks!
You need to setup the dialog with the UI you from your .ui file. The Qt uic compiler generates a header file from your .ui file which you need to include in your code. Assumed that your .ui file is called about.ui, and the Dialog is named About, then uiccreates the file ui_about.h, containing a class Ui_About. There are different approaches to setup your UI, at simplest you can do
#include "ui_about.h"
...
void MainWindow::on_actionAbout_triggered()
{
about = new QDialog(0,0);
Ui_About aboutUi;
aboutUi.setupUi(about);
about->show();
}
A better approach is to use inheritance, since it encapsulates your dialogs better, so that you can implement any functionality specific to the particular dialog within the sub class:
AboutDialog.h:
#include <QDialog>
#include "ui_about.h"
class AboutDialog : public QDialog, public Ui::About {
Q_OBJECT
public:
AboutDialog( QWidget * parent = 0);
};
AboutDialog.cpp:
AboutDialog::AboutDialog( QWidget * parent) : QDialog(parent) {
setupUi(this);
// perform additional setup here ...
}
Usage:
#include "AboutDialog.h"
...
void MainWindow::on_actionAbout_triggered() {
about = new AboutDialog(this);
about->show();
}
In any case, the important code is to call the setupUi() method.
BTW: Your dialog in the code above is non-modal. To show a modal dialog, either set the windowModality flag of your dialog to Qt::ApplicationModal or use exec() instead of show().
For modal dialogs, you should use exec() method of QDialogs.
about = new QDialog(0, 0);
// The method does not return until user closes it.
about->exec();
// In this point, the dialog is closed.
Docs say:
The most common way to display a modal dialog is to call its exec() function. When the user closes the dialog, exec() will provide a useful return value.
Alternative way: You don't need a modal dialog. Let the dialog show modeless and connect its accepted() and rejected() signals to appropriate slots. Then you can put all your code in the accept slot instead of putting them right after show(). So, using this way, you wouldn't actually need a modal dialog.

Qt doesn't display child widget

How can i access ui files of children of a class. Lets say MainWindow class has twoa child dialog. I want to access LINEEDIT of dialog so that i can take text from there. Similarly how can i access ui files of parent inside child class in QT. Note: I havn't inherited any thing from Parent class.
I have writen the following code, in order to display a dialog but it won't show!
void MainWindow::displaydialog()
{
ItemDialog dialog= new ItemDialog(this);
dialog->show(); // it is not displaying the dialog
}
and how can i access the ui widgets like check whether ListWidget item has been selected or not.
Here is the code of itemdialog,
#include "itemdialog.h"
#include "ui_itemdialog.h"
#include "mainwindow.h"
ItemDialog::ItemDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ItemDialog)
{
ui->setupUi(this);
setWindowTitle("Status Dialog");
setFixedSize(QWidget::sizeHint());
}
ItemDialog::~ItemDialog()
{
delete ui;
}
void ItemDialog::on_pushButton_clicked()
{
MainWindow obj;
obj.okbuttonclicked(ui->lineEdit->text());
}
Please review an example such as this: http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
It explains how to use the ui files that you generate from Qt Designer. You shouldn't really think of them as "ui files" in the sense of accessing them on the widgets in your class. The idea is that you include them, and then use their setupUi() function to apply them to your given class. At that point, everything you created in qt designer, and that is in that ui file, is now a member of your class. They can be accessed via the naming you used in qt designer.
As for why your dialog isn't showing...I don't know because you only included 3 lines of code as an example. Theoretically it should show if Mydialog was properly set up. You could just try changing it to a QDialog to make sure you didn't do anything wrong with your custom class.
It depends what you want that dialog for. Either it's a modal dialog - some kind of a information display or retrival that blocks the function of your program until user reacts, or it's somekind of toolbox or similar, in which case you probably should not use QDialog.
If a modal dialog with a line edits and/or additional features is what you want, you should read up on QDialog in the doc. See the exec() function. Basic usage would go like this:
void MainWindow::displaydialog()
{
ItemDialog *dialog = new ItemDialog();
if (dialog->exec() == someApropriateReturnStatus)
{
QString somevalue = dialog->someValue();
int dialog->someOtherValue();
//do something with the value
}
delete dialog;
}
The point is that the ItemDialog class handles the UI internally and implements the getter functions accordingly, you should not (in most typical cases) access it's UI from outside.
If a simple line edit is all you want, you'd be better off using one of the standard dialogs already implemented in Qt, have a look at the Standard Dialogs Example

How to call created .ui design form in Qt with button.clicked() event?

I'm very new in Qt and I would like to ask how to call the created .ui designer form to be connected with the PushButton in MainWindow.
What I've done:
1. Created a new .ui design form (Forms.right click -> Add New -> Qt Designer Form) to the current project.
2. Designed an about dialog in the .ui form created.
3. Make a slot PushButton.clicked() in the MainWindow.
For all who have done programming in Qt, please help me solve this problem to connect the PushButton with the .ui form, so the PushButton can call/show the form created in the .ui.
Thanks for your attention.
Using .ui files is explained in Qt's documentation:
http://doc.qt.io/archives/qt-4.7/designer-using-a-ui-file.html
Summary:
The easiest way to handle an .ui file is to run it through uic (the User Interface Compiler) at compile time. This is done automatically if the .ui file is included in your project (.pro) file. ("Add New" probably has done this automatically in your case.) Then you only need to include the generated C++ header file in your source file. Its name should be something like "ui_nameoftheoriginaluifile.hpp". Of course, after that you need to instantiate the form defined in the .hpp file.
Edited to add:
There are several issues with your code, starting from its readability. I don't know if you have used an object-oriented language before, but a very basic rule in C++ is to begin class names with capital letters to make them easier to distinguish from objects and other variables. So the class names should be "About", "Parent", etc.
The compilation error is caused by using "about" instead of the name of the class actually used in the "ui_about.hpp" file - "MainWindow". (Which is determined by the name of the form you have used in the .ui file.)
If you are using Qt Creator: hold down the Ctrl key and click on the name of the file "ui_about.h" in the include directive. This will open it for inspection. Try to figure out how it works.
You also haven't defined the opDialog() function as a member of the "parent" class in "parent.cpp", which causes another compile-time error.
You also shouldn't be using a QMainWindow for an about dialog. QMainWindow is supposed to be the main window of your application - there shouldn't be any more instances of it.
So, about.h:
namespace Ui {
class MainWindow;
}
class about : public QMainWindow {
Q_OBJECT
public:
about(QWidget *parent = 0);
~about();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
};
And the beginning of about.cpp:
#include "about.h"
#include "ui_about.h"
about::about(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}