How to create a multiple window GUI in Qt [closed] - qt

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am very new to this, it's my first GUI project. I want to create a GUI in Qt that has multiple windows in which I have buttons that take me from a window to another. Also, across these windows, I will be passing data.
I am using Qt Designer, and I was wondering what would be the best way to do this. Do I create multiple Main Windows for example? Or should they be as Widgets? Or some other way?
Any help would be appreciated.

The main window is one by definition. If you need to create multiple windows however the simplest thing to do is to create a new widget without any parent and show it with show().
QWidget *fstWin = new QWidget(nullptr);
QWidget *scdWin = new QWidget(nullptr);
fstWin->show();
scdWin->show();
https://doc.qt.io/qt-5/application-windows.html

Your question is very basic. Seems you started to learn GUI from the beginning and probably the programming at all.
Anyway, you dont need more than one Main Window. This is where your program starts.
All other windows must be created from the designer. They are Dialogs. To add a new Dialog, you need to click on your project (right pane), and select Add New...
From the dialog, just select QT and then Qt Designer Form Class. Follow the Wizard and set the type and name of the dialog.
Finishing the Wizard, will add you three items in the project with the name you selected: Header File, Source file (.cpp) and Form file (.ui).
To show a the newly created form, you need to #include your .h file in the mainwindow.h then you need to create a member variable of type "your newly created class" in MainWindow class.
To show the new dialog, just use the function .show() from the member variable. To show the dialog in modal state, use the .exec() function of the member variable.
To pass data between windows, you need to make corresponding methods in your newly created class.
But, just before doing all this stuff, I advise you to take some lessons about Object Oriented Programming. You have to know, what is Class, Object, Member and Field/Variable
Or it will be very hard for you learn more.

Related

Edit multiple QT widgets (GUI) in QT Creator

I have a general GUI editing question using QT. We can create GUI applications using the QT form view where we can edit the layout graphically. And then we can swtich to the code to fine tune. Problem is i can Edit only the main window GUI. Now if i want to create another dialog e.g, pop up, i could only do so from code. Can you recommend any example or direct to the QT Documentation which shows how to edit multiple widget/dialog windows graphically using IDE.
I looked through the questions asked here which neatly explains how to open a new dialog window with code examples. But i could not find any example showing how to edit multiple layouts using QT's graphical editor.
To add other forms to your application, right click on the project name in the project tree view, then select "Add New", then under the Qt category look for "Qt Designer Form Class", like in this picture:
Follow the rest of the wizard (will ask the class name) and in the end you should find tree more files in your treeview with extensions .h, .cpp and a .ui. Click on the latter one to open the designer and edit the form from there.

Is it possible to create a Qt customer widget *in* designer *for* designer?

I have found a really neat custom widget for Qt and managed to incorporate it into Qt Designer. I would now like to create a customer widget in designer by placing 32 of these LEDs into a new widget in Designer, then add some functions to it such as giving it address and passing data into it that will then illuminate patterns in colours according to the passed data.
My questions are:
Can I do this? I have placed 32 LEDs on to a new QFrame widget in designer and named the LEDs 0-31, but am at a loss as to know what to do next.
Is there a tutorial anywhere for this? I have found loads of hits on google for creating custom widgets for designer, but not using designer.
Please bear in mind I am not a good Qt programmer, I write embedded code for micros in C and am really not good at object orientated code; I can just hack together relatively simple programs for test and measurement. I need something that will hand hold me through this.
http://doc.qt.io/qt-5/qtdesigner-worldtimeclockplugin-example.html
This seemed to be the kind of thing I wanted, it seemed to look like someone has placed widgets on a screen to create a new widget, but it seems to keep referring back to Creator.
Maybe I am trying to do something impossible. Maybe I have to place this new LED in creator with code?
Many thanks for any pointers.
Yes, it is. A Designer plugin basically consists of 2 Parts:
The Widget that you want to add to the designer
The plugin itself that provides information about the widget usw.
You can simply create the widget with the designer, add the QDESIGNER_WIDGET_EXPORT macro and thats it. Create your designer plugin to load this widget and it should work.
Note: Since you mentioned your not very experienced, you should try to follow the Custom Widget Plugin Example. Once you understood that one, it's only a small step to your designed plugin.

QML and Qt Creator

I designed a Qt application by Qt creator. As you well know when you build a new form is possible to drag an drop the default items inside the main window. Instead of use classical "push button" I created a custom button by adding .qml file to the project. The problem now is that I don't know how I can use (or integrate) the new button inside the form of my project.
Thanks in advance
As long as your component is in the path of your application qml files, all you need to use your component is to place it somewhere. You don't need to include or import anything. Any user component is directly available to the entire project.
As long as the QML component is made from only built in components, it can even be safely loaded from arbitrary location on disk, over network or just from a source string. Check this answer for details on dynamic instantiation.
A friendly advice - type the code, do not use the visual editor - it is pretty weak.
EDIT: I don't know about you, but for me, seems like every custom qml file in the project qml folder is automatically added to the QML types in the designer library. So contrary to what I assumed, you shouldn't really need to do anything to get your custom type available for use in the designer.
I found this 1 year-old question when facing the same problem. This is what I found out:
If you save your custom button with an initial uppercase letter (CustomButton.qml instead of customButtom.qml), QtDesigner shows your component in the library panel correctly.
Sometimes you need to restart QtDesigner to work.

Adding user built widgets to a ui file in qt creator

The qt designer portion of qt creator has many built in widgets. But let's say I want to add custom widgets created in the same qt project to the ui file of the window. By taking these steps:
Create a new Qt GUI application with a main window, we'll call the window A.
Add a new widget to the project, the widget just uses standard UI components, say buttons. We'll call this widget B.
Add an instance of widget B to window A.
Now, I know one way to do that, and that is:
In window A, add a blank widget (or widget container, from the containers section of the list of possible widgets. We'll call this widget C.
Promote it (widget C) to widget B.
However, the problem with this is that Qt Creator's designer treats it like a generic QWidget. And as such, you can't do things like add it to a splitter, or connect signals/slots that are specific to the widget.
So is there any other ways to add widget B to window A in the ui file using qt creator? Thank you.
I'm not sure to understood your question well so I could ask the wrong question. Are you sure your "B" widget is a subclass of QDesignerCustomWidgetInterface? This should expose all stuff that your widget/plugin offers...
Last note: a friend of mine tried to add a custom widget like you. And at the end of the described procedure that Lol4t0 told you, he found you must compile plugin with the same compiler with wich qtcreator/designer was compiled. This happens because as we know c++ doesn't keep ABI compability (instead of i.e. C language) stuff like: Name handling can change from compiler to compiler, how data is loaded into registers can change...and so on. My friend tried to compile plugin with mingw, but he found that qtcreator was compiled with visual studio compiler. Therefore if you want to deploy your plugin on Windows or you compile your plugin with visual studio, or you have to compile qtcreator/designer from scratch.
I know this is a very old question, and I'm not sure what capabilities Designer had in 2012, but I came across this in a Google search for something else and figured I'd add some missing info:
However, the problem with this is that Qt Creator's designer treats it like a generic QWidget. And as such, you can't do things like add it to a splitter, or connect signals/slots that are specific to the widget.
Generic QWidgets can be added to splitters with no issues these days.
As far as signals and slots go, you can use them like so:
After promoting a widget to your custom widget, right-click it and choose "Change signals/slots..." from the context menu.
Add the signatures for any custom signals and slots you want to be able to use in Designer / Creator here.
Now those signals and slots will be accessible in Designer like any other signals and slots.
The only thing you can't really get with promoted widgets is access to custom properties in the property panel; for that, yeah, you'll need to go through the custom widget creation process.

Modifying Qt core components/widgets, best practices? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to enhance Qt's QPrintPreviewWidget by allowing it to display page numbers (in the footer somewhere). Unfortunately, I can't quite figure out how to go about it without hacking up Qt's source. I see a great spot for this additional code (in qpaintengine_preview.cpp, in newPage() method) but that means I'm going to have to recompile the whole of Qt (I got a binary build from Trolltech). Furthermore, if I decide to run an app that uses this functionality on someone else's box, I'm going to have to recompile there as well (say if it's a different arch).
Are there any other cleaner ways?
Thanks
Is the newPage() method virtual? If so, you could subclass and use that in you own applications, which would be a bit easier.
The second option would be to statically link your executable with the modified Qt libraries. You need to be aware of licensing concerns to do this. This way, every place you put the app will have your modified functionality. (You would still need to recompile for different architectures.)
Finally, you could get the latest sources from http://qt.gitorious.org/, modify them in the way you desire, and submit a patch back to the trolls. If you do this, you'll probably have to keep the old behavior the default, and add an option to enable the new behavior. They may or may not accept the patch. And if they do accept the patch, you may not be able to rely on other people's computers getting that version for quite some time, if ever.
Read the source code.
In this case, read QPrintPreviewDialog source code to see how it does it. This standard dialog has navigation buttons and a current page display, so it kind of does what you want (that is, if I really understood what you want to accomplish). The methods you're looking for are the following (src/gui/dialogs/qprintpreviewdialog.cpp):
void QPrintPreviewDialogPrivate::_q_previewChanged()
void QPrintPreviewDialogPrivate::_q_navigate(QAction* action)
void QPrintPreviewDialogPrivate::updateNavActions()
Basically, _q_previewChanged() is connected to QPrintPreviewWidget::previewChanged() signal. When it is emitted, the page number is updated with information acquired from QPrintPreviewWidget::currentPage() and QPrintPreviewWidget::pageCount().
As for extending the behavior of QPrintPreviewWidget you can try two approaches, both of them do not require a tailored version of Qt:
Extend QPrintPreviewWidget
In the constructor, access the layout() (it is a QVBoxLayout that is used interanally), add the footer widget, connect the previewChanged() signal to a slot that updates the page number and be done. The problem with this approach is that it counts on the layout to be present and be a QVBoxLayout. Since this is somehow private, it can break with newer versions.
Create a new class extending QWidget or QFrame
If you don't require your widget to be a QPrintPreviewWidget, just create a new QWidget derived class and add the print preview widget and the footer to a layout, connects slots etc. Use your derived widget instead of QPrintPreviewWidget.
Now, if you want to modify the behavior of the widget on already deployed binaries, things get uglier. I cannot help in this case.
If you have a library and this library has same symbols linker at runtime is looking for - to get the code for the print preview - then you can inject your own code to replace the real implementation. Method is called dll injection.
Check http://en.wikipedia.org/wiki/DLL_injection for more info

Resources