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.
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.
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"
I want to build a GUI on top of ndpiReader application that comes with nDPI library. My question is:
1- Is it possible to use Qt's QProcess to create a GUI for this application is written in C and uses a library (nDPI) that is also written in C? This is very important question for my work.
2- I checked this question with the same title as mine, but it doesn't specify the steps for a beginner like me how to set up the project in order to kick start. From QProcess documentation page I can see that one can send arguments for the target application through QProcess method start() but I'm somehow confused, this is my first time trying to write a GUI for a console application, Do I need to include any part of the target app's source code in my Qt project? What type of project should I choose in Qt Creator when I first create a project? (I personally guess I should use Qt Widget Application, but I need confirmation of that).
Thanks for any help.
I've always been developing simple(console) apps. And even then most problems I had was with starting/porting/CMaking/ libraries to work.
I need to find a gui which is added/used by adding #include "somelibrary.h" to EXISTING c++ project. I've downloaded QT, but it seems I have to create a new QTproject,... and thought alone of including all CUDA,OpenCV,others is making me sick.
I've been experimenting with windows forms (.net?) but there is this managed/unmanaged border with its creepy bugs.
So I'd like to add GUI to existing project (where forms can be designed in completely separate designer).
Do you know any?
Or maybe You'd suggest me different approach?
I suggest you to use CMake and Qt. CMake is better than qmake to manage projects and use additional libraries. Currently Qt is the best multi-platform GUI API.
QT and winforms can be added on to an existing project but it's harder that taking a gui application and adding your project to it. Event driven code is organized differently than procedural code.
You don't need CUDA or OpenCV for a GUI.