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.
Related
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'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.
I am going to be making a project with OpenCV and (probably) Qt for GUI and I was just wondering what kind of project I should create in visual studio? Would it be the Win32 Project or Win32 Console Application or just an Empty Project? A lot of them are quite obvious as I am not making a Dll or makefile but do not know the difference between the others. Thanks in advance.
Which type of project you decide to pick in Visual Studio depends what will best fits your needs. Since I do not know your exact needs, the best answer I can give is explain the difference between the types of Project Options and give examples when you would choose to use the given project:
Win32 Project: A Win32 Project is one of the options Visual Studio provides to you, and if chosen will provide the user with template code that generates a "window", that window being just like any other window (browser window, folder window, etc), but one that your program controls.
Examples: Making a calculator, making an application that needs a window with buttons for the user to communicate with the program, etc.
Win32 Console Application: In this option, Visual Studio provides a Command Prompt interface where the user can input data and also where output can appear. Basically it's the black window where you can input commands and receive information of what is happening in your program.
Examples: When testing out code (it makes debugging easier in some cases), when the program really doesn't need to be too elaborate, etc.
There are other options available, thou these two were mentioned in your post and are probably the most popular among developers.
After installing Qt and maybe a Qt-VisualStudio-Plugin you have some new options when creating a New Project:
You should choose Qt Application in most cases. Not sure if you can use a GUI with Qt Console Application, but you'll get a terminal/console in that case.
After choosing project name, you can easily choose the Qt Modules you'll need. This adds them to your project settings, so you don't have to add the manually (but I guess you can do so later if you need more modules).
The project will create a .ui file which you can open/edit with QtDesigner. The project will perform all the moc and uic compile steps automatically and you don't have to add those things manually.
I think this is the easiest way to use the combination of Qt and Visual Studio.
Adding OpenCV to Visual Studio is easy:
Just add the include directories and the correct OpenCV libraries.
The question whether to use a consolse application or not depends on your needs.
Personally, I like printing development output to a console, but maybe you don't want that in your final project ;)
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
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.