Creating, handling and switching between windows QTCreator - c++

My research into the question suggests that I'm somewhat beating a dead horse, however I can't seem to get a conclusive answer.
I'm using QT Creator to create a GUI that will help interface with a register of units(simple objects, with some ID's and such).
I have a Main Menu, which contains 5 push buttons and a table. Pictured here. My project currently includes these files, and my main currently looks like this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
UnitRegister e;
CommInterface c;
w.setRegistryPtr(e);
w.setCommPtr(c);
w.setTablePtr(w.findChild<QTableWidget*>("unitTable"));
w.show();
return a.exec();
}
What I want to do is press one of the four push-buttons on the left, and use that to switch to a different view in the main window.
For this purpose I considered using a QStackedWidget, and then having a page for each menu button. My question is this:
How do I/Should I create a separate QT Item Form Class(header, .cpp
and .ui file) for each page
How do I switch between the pages I've added using the QT Designer.
How do I ensure that the different pages can access the UnitRegister created in my main file.
I'm not necessarily searching for a complete answer, but something to get me going.

Switching Pages: Consider a QButtonGroup to give your buttons ids that you can map to the indexes of your QStackedWidget. Then you can do this:
connect(buttonGroup, SIGNAL(buttonClicked(int)), stackedWidget, SLOT(setCurrentIndex(int)));
Organizing the Pages: Create a .ui file and corresponding container widget for each page in your widget stack. This is much easier than one massive .ui file.
Accessing the UnitRegister: There are tons of ways to do this. Adding a setter function to your classes is one way.

Related

How does Qt find widget instances?

At the first, Qt isn't my primary framework as well as C++ isn't my primary language, so my question may be very easy but it's unclear for me from tutorials.
There is a simple sample in official tutorial:
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
But they don't explain how button links with app to it could be shown and wait events. I can imagine there is a some singleton and button, and app find each other via that singleton or there is a some preprocessor that finds widget instances and automatically links it with app instance. But I want to know real situation. How are they linked together?
P.S. I am sure someone asked a similar question already but I couldn't find. If you know any duplicate of my question, specify it, pls, I will delete my topic then.
Your guess is almost correct despite the fact that the "singleton" is the QApplication itself.
By reading the QApplication's documentation, you can see that only one QApplication object has to exist.
According to the documentation again:
"It handles widget specific initialization, finalization."
Then to answer your question, I know that there exists a global pointer to the QApplication instance that you can access via qApp.
I'm pretty sure that this is what it is used by any QWidget to find the QApplication object.

QT 5 add a custom widget inherited from QGraphicsView to designer

I have a custom widget from modified elastic nodes example. It inherits from QGraphicsView. In the example it is set as a central widget in the main window, so nothing can be added. Here's the code for main from the example:
#include "graphwidget.h"
#include <QApplication>
#include <QTime>
#include "mainwindow.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
GraphWidget *widget = new GraphWidget;
QMainWindow window;
window.setCentralWidget(widget);
window.show();
return app.exec();
}
I want to add the widget to the ui form, so that i can add some buttons and input. How can i do that?
What you are doing now is making an application.
If you want to make a custom widget in Qt , you need to build it as Custom Designer Widget - a plugin (as Dll).
In Qt creator choose OtherProject=>Qt Custom Designer Widget and create a project. You need to build as Release version, not the debug one.
After building, if Qt hasn't already, you need to copy the .dll file into Qt/plugins/designer and Qt/Tools/QtCreator/bin/plugins - you will find .dll's there. You also need to build plugin with tool chain, as Qt creator/designer was i.e. VisualStudio 2013 64bit or Mingw - MUST be the SAME.
It's not easy, but i wish you luck.
An easier way is to add QGraphicsView in the ui form and promote it to GraphWidget class.

Run a method from a C++ program with Qt without writing entire program in Qt [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is it possible to run a certain method from a C++ program with Qt with certain input parameters when for instance a button is clicked without writing the complete program in Qt itself?
Let's say that I have a project in Visual Studio with a corresponding .exe and I want to run the method example with input parameter 5 whenever a button is clicked. How could I do that?
Qt operates by creating a QWidget window, and then an event loop.
Your code to create a window, and then a button that triggers another function is trivial.
Many of the examples Qt provides show this functionality.
To get started, Download Qt. Download the Qt Add-On for Visual Studio. Note, that the express edition of visual studio doesn't work with the Qt Add-On.
After you have all that done, create a Qt project from the Qt classes that are available. In this case, I would not choose QML/Qt Quick, but just a Qt Widgets class, such as QWidget or QDialog or QMainWindow.
Using the Qt Add-On should ensure that you have the libraries found and available to Visual Studio.
Make sure that you can get a Hello World example is buildable and running using a Qt Widget of some sort.
The code it generates for you should look like this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To add in a button, you need to place it on a Widget and then connect a SLOT to its clicked() signal. The following is a compact way to do it.
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>
#include <QApplication>
static void someFunction ()
{
qDebug() << "Button was clicked";
// run my other function that requires a parameter of 5
qDebug() << "Run my other function with 5!";
// otherFunction(5);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton *button = new QPushButton("Click Me");
QObject::connect(button, &QPushButton::clicked, someFunction);
QVBoxLayout *vbox = new QVBoxLayout();
vbox->addWidget(button);
w.setLayout(vbox);
w.show();
return a.exec();
}
To make a powerful GUI, you will want to leverage more than just a functor, and use QObjects and let it use the moc compiler and create your own signals and slots for all your custom functions.
Connecting to a functor is new with Qt 5.
http://qt-project.org/wiki/New_Signal_Slot_Syntax
http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html
A more verbose way to do it, but allows for more expandability and OOP, is to subclass QWidget or QMainWindow, like it generated for you, and put a method in your subclassed function that you put under your slots list in your header file.
http://qt-project.org/doc/qt-5/signalsandslots.html
Take the time to understand signals and slots and you can create almost any GUI you can imagine using Qt. Good luck.
Hope that helps.

Qt: Access Widget from main function and implement exit button

I want to implement an exit button in my application, which has the following setup:
I have a main function which looks like this:
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
I also have a mainWindow function which has been generated by the QT Creator IDE.
I design the GUI with the Qt Designer and when I want a pushbutton to do something when clicked, I use a function like this:
void on_selection_clicked();
I hope the setup is now sufficiently described.
Now to my problem: I want to implement a button, which, when clicked, terminates the window and the application. I first tried implementing this in a function like this:
void on_exit_clicked();
But I don't know that to to here.
Then I heard of a aproach via QObject::connect, but I have two questions:
1.) Where should I put this? In the main function? 2.) can I access the object simply via the object name given in the QT Designer?
no you should connect it in the constructor of the MainWindow
connect(ui->exit,SIGNAL(clicked()),QCoreApplication::instance(), SLOT(exit()));
QCoreApplication::instance()->exit() will quit the application
yes through the ui field in MainWindow see the code above
I don't know which Qt version you use, so I will suppose Qt 5.0 (signal/slot mechanims was updated).
QWidget has slot QWidget::close().
QPushButton provides signal QPushButton::clicked(bool checked = false)
So you can connect them in constructor of your MainWindow:
QObject::connect(your_button, &QPushButton::clicked, this, &QWidget::close());
Also I suggest to look into the files generated from *.ui files - so you have deeper understanding of what's going on.

Using Qt To Build A Full Screen Layout

I am trying to use qt along with the qt designer to create a simple full screen webview with a line edit and a button above to create a really simple browser.
The problem is that the layout doesn't want to expand to fill all of the available space. I seem to think that I am missing something really simple, but I just can't seem to figure it out.
Here is an overview of my layout
<MainWindow>
<GridView>
<VBoxLayout>
<HBoxLayout>
<lineEdit /><PushButton />
</HBoxLayout>
<WebView />
</VBoxLayout>
</GridView>
</MainWindow>
Here is the code on the MainWindow Class
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit->showFullScreen();
ui->pushButton->showFullScreen();
ui->webView->load(QUrl("http://google.com"));
ui->webView->showFullScreen();
}
and here is the main code
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
It comes out looking like this instead of the webview and lineedit filling the whole amount of space
Is there anyway of doing this in Qt, I would have assumed that this was a standard thing to want to do.
Many Thanks in advance!
I think you have to replace the GridView by the VBoxLayout directly above.
What is the grid view you use exactly ?
<MainWindow>
<VBoxLayout>
<HBoxLayout>
<lineEdit /><PushButton />
</HBoxLayout>
<WebView />
</VBoxLayout>
</MainWindow>
Make sure your Object Property "sizePolicy" is set Maximum and "maximumSize" is 16777215, then w.showFullscreen should work
If you mean that you want the window to fill the entire screen, hiding the task bar and title bar, you should replace this:
w.showMaximized();
with this:
w.showFullScreen();
If you instead mean that you want your line edit and web view widgets to fill the window that contains them, "full screen" doesn't mean what you think it does. Your problem is probably related to your layouts in designer, and we don't have enough information in your original post to properly diagnose it. Given how simple your UI is at this point, the easiest thing to do is probably break all layouts and start over.
Whatever it is that you want, you definitely shouldn't be calling showFullScreen() on your line edit, push button, or web view.
I suspect the problem is that you've set up the BoxLayout inside the parent widget, rather than setting the parent widget to use a BoxLayout. In Qt Designer, you should be able to right-click on the parent widget and set the layout, and it will expand like you want. If you just add a BoxLayout to a widget from the toolbar, it won't automatically expand to match its parent.
See this link for more details: http://embrisk.com/notes/qt_resize.html
Expanding upon Stephen Bell's answer (I'd comment but I lack the reputation), as it took me awhile to figure this out as well.
If you have completely designed the layout you wish to have fullscreen, right-click on the "Main Window" in the far right of the screen. A pull-down should appear. Click Layout, then Grid Layout. Main Window should assume a grid layout. It should not change the positioning of your objects at all in Qt Designer. Yet, it should fix the fullscreen.
If MainWindow does not have a layout, your layout will likely have whitespace or deadspace without an explanation.