Qt5 C++ Link New Window with Qt Designer - c++

I've been trying to figure this out for the past 3 hours.
Honestly I hesitated to ask this question but it seems like the specific thing of mine isn't going to be answered anywhere. So here's my situation:
TL;DR: How to link newly created windows (either in code or in Qt Designer) with the corresponding part. Means, how to link code- generated window and Qt Designer and the other way around?
Right now I am working on an Window Application with Qt5 in Visual Studio 2017.
I want a new window to open up when I click on a button, and then edit it with the Qt Designer, like I do with the main window.
(Have in mind I'm using Visual Studio 2017 mostly for programming.)
I was able to do exactly that. But I have no way and idea on how to access the "class" and the whole window with the Qt Designer.
I figured I need an *.ui file to feed it into the Qt Designer, but I have no clue on how to create that.
Also, I read through the whole documentation, but didn't get how to apply the knowledge at all.
So, my code to open a new window looks like this:
mainprog.h:
class Program: public QMainWindow
{
Q_OBJECT
public:
Program(QWidget *parent = Q_NULLPTR);
private:
Ui::ProgramClass ui;
Ui::ProgramClass * uip = &ui;
private slots:
void on_pushbutton_settings_released();
};
mainprog.cpp:
Program::Program(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(uip->pushButton_settings, SIGNAL(released()), this, SLOT(on_pushbutton_settings_released()));
}
//-----------------------------------------------
// Related function
void Program::on_pushbutton_settings_released()
{
qDebug() << "on_pushbutton_settings_released";
QWindow *settings_window = new QWindow();
settings_window->show();
}
So this is how I can create and display a new window (here: settings_window).
But how to "access" it's class and it's methods and edit it in the Qt Designer? If I'd be in Qt Creator, there would be no (or at least less) problems, because there are plenty of tutorials and Qt Creator creates all needed files automatically.
But I'm working in Visual Studio 2017 and thus I can't use all the utilities of Qt Designer/Creator/etc.
I thought I may create specific header- and *.cpp- files. But then again, what should I put in there to do everything? I found out that there is always a "ui_Program.h"- file and that this one is created by reading the *.ui file.
But still, even if I created such things, how to make it read everything?
Or maybe I'd ask the other way around: How to use/link a newly created window in Qt Designer in my own code in Qt5 C++ with all related classes, headers, etc.?

It appears that the simplest solution is to use Qt VS Tool extension. The tool should handle creation of .ui-based widgets and custom build steps (I haven't used it myself, hopefully will work out of the box :).
There are other alternatives, such as using a third-party build system with Qt support (like CMake or Meson) or setting up pre-build steps manually, but that's a broader topic.
I think you'll prefer the above approach, but I'll try to answer you questions directly, too.
I figured I need an *.ui file to feed it into the Qt Designer, but I have no clue on how to create that.
Just open Qt Designer and use the "New Form" dialog (alternatively, click "File > New" to open it), and save the file.
.ui files are XML files. In order to generate code, you'll need to use Qt's uic tool (stands for UI Compiler) to generate your ui_<something>.h file. Typically, this is done automatically - the tools I mentioned in the first part do exactly that each time a .ui file is modified.
Once you have this generated header file, you can use it the same way you do in case of Program class: derive from QWidget (QMainWindow is derived from it, too), and call setupUi in the constructor.
One more thing: QWindow is not what you want. For GUI applications, you need to use QWidget instead. QWindow is used internally by (top level) QWidgets and sometimes is used directly (for example, when there's a need to use third party rendering, instead of Qt's APIs). You can read more about it here.

so for anyone trying to solve the same problem I described in my question, I found an answer.
You need to have installed Qt Creator, but that should usually be installed if you have Qt Designer and use it.
Make a new project in Qt Creator, name it whatever you want.
Do it like if you would code in Qt Creator, meaning go to you project file name, right- click, and "Add New...". (Projectname.pro -> Right click -> Add New... -> Qt -> Qt Designer-Formular-Class (or whatever its called in english) )
Name it the way you want it to have in your VS- Project.
Save everything, close Qt Creator.
Navigate to the folder where you saved your *.pro file (Qt Creator project file) and pick up the class- related files.
classname.h
classname.cpp
classname.ui
Copy these files into your vs project folder
Drag and drop or integrate them into you vs- project, like you would usually do with "normal" additional files.
Correct all #include's and everything thats needed.
Don't know if this is necessary but open up the *.ui file with Qt Designer in your project browser in vs.
Now you can use the following code to implement the new window (QWidget) in your project:
classname new_widget; // creating new widget
new_widget.setModal(true); // isn't necessary, more info in the video (link) I post below
set_wind.exec(); // execute new widget (or whatever)
This may not be the most professional way but this is how I figured it to work for me. It's a tedious process with multiple windows, thought.

Related

How to add .ui file in Visual Studio (2010) project and compile it

I'm new in using Qt and I have a problem. I have created a simple window (.ui File) with Qt Designer and saved it.
Now I want compile it with Visual Studio 2010 . How can I add an external .ui file into my Visual Studio Project and compile it?
Should I create a new class in the project? If yes, how?
The best option (and probably the only one unless you are using CMake or similar) is to use the official Visual Studio Qt add-in (download from here): it will handle all the pre-processing steps required by some of Qt functionalities (C++ classes moc'ing, UI compilation...). It will also associate Qt files (.ui, .qrc, .ts) to respective editors.
Basically, you create a new Qt project, then create the .ui (you can use the one that comes with the template), create a class that inherits from the respective widget (QWidget, QDialog, QMainWindow) (again, there is one with the default project). From that class you setup the UI (you link the .ui and the C++ class, let's say), create slots and connect them with the UI elements, etc.
For a step-by-step tutorial please take a look at the official manual, specially the getting started section.

dynamically creating GUI in QT without using forms in visual Studio

I have installed QT5 in visual studio.
I want to create my GUI dynamically at run-time. Hence I cannot use any designers or forms. How do I do that? Which template should I create in visual Studio? Also which QT libraries do I have to include to achieve the same?
Designers and forms are only helpers that end up with generating C++ code that you want to write yourself. So you can create a form (in designer), build your project and see generated code, from which you can learn how to create and setup UI objects. You can then leave those forms aside and write your code using generated one as code snippets.
Anyway, the short answer to you question "how to create my GUI dynamically at run-time" is: create objects of UI classes (such as QMainWindow) and manipulate then using Qt API
Which template should I create in visual Studio? - C++, Win32 Project
which QT libraries do I have to include? - Again, use Qt Creator as a "teacher". Once you have a project built in Qt Ctreator, look at its "Compile output" window. From there you will learn what compiler and linker settings are needed
Practice building the forms in Qt Designer. Then go to Form -> View Code and look at the layout code. This is an example of the C++ code that you can use yourself to build widgets at run time.
When you write your own widget without designer, you can simply subclass QWidget and add buttons, dropdowns, etc. Or you can try overriding paint events to do custom painting.

how to use Qt Creator source code in my project to create a file management system like Qt's project

In my recent project,i need to accomplish a file management system like Qt Creator's "Projects part" in the left-top .I tried to use QTreeView to implement it,but it's effect is far less than my expention.Since Qt Creator's source code is open,can i use it's code and make some needed change? And can you tell me which files to refer to ? I am using Qt4 in my project.
this picture shows want i mean "projects part"

Qt designer and Creator

I have experience in C++, but I am totally new to Linux programming.
I figured out how to build a GUI, in Qt Designer, but I want to subclass QTextEdit before I create the interface, so I can create my own slots. If I use Qt Creator first, then my code doesn't show up when I switch back to Designer.
Could someone please explain the relationship between Designer and Creator, and how I maneuver between the two?
to keep things simple..
QT Creator it's nothing more nothing less then just an IDE (btw it has Qt Designer components build in, but I still prefer standalone designer).. Editor, front-end to GDB, project management.. mostly everything you can find in other IDE's
QT Designer - user interface editor which produce an XML files which can be used in two ways. 1) process them with MOC preprocessor (part of Qt) which generates you C++ classes for your user interface components 2) (which I prefer much more) load them runtime

using makefile in QT creator

I have a C++ project which uses Makefile,now i want to use QT creator for development but I do not want to touch the Makefile, Is there any way QT creator with an existing Makefile.
When you make a new QT Creator project, you are able to edit the make file in the program.
I haven't not had any trouble with adding in my own files, libraries ect. into it, from another, non-QT project.
If you want to avoid the make file all together there is an import function in the GUI to bring in outside files, but I would keep an eye on what it is doing. I have had it make 'creative' choices on what to do with files.
Edit To Include Answer: You can change the exact steps that are used to make the project in the project tab on the left side.