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

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.

Related

How can I connect functions on an hpp and cpp file into qt design studio?

I am currently working on a project where my task is to work on the development of the UI, which I have never done before. I am encountering a lot of issues, especially with merging files from my other teammates who are working in hpp and cpp files and I must take the functions in those files to create a functional UI.
For example, what I am currently working on is creating the desktop application for the login of users, and some teammates have created functions for password encryption and authentication mechanisms that I need to merge. I notice that the files on QT creator are just .ui while on QT Design studio they are ui.qml (maybe it is the root of the problem but I don't know how to fix it)
I have already tried to add the hpp and cpp files on QT Design studio (version 6.4 of everything) but I don't see any places where I could add them and merge them. Then I tried opening and running the QT Design Studio login file on QT creator and it does not run, it just gives errors that I do not understand, such as: ":-1: error: ninja: build stopped: subcommand failed."
Is it possible to do so on QT Design Studio? If so how? If not, how do I transfer my work done on QT design studio onto QT creator? Thank you in advance for your help.
QT Design studio is just for UI designing...
The general workflow with QT C++ and QML is
to create the logic in C++ and call that logic from QML.
That is usually done via:
backends
registering C++ types to the qml engine.
Note that anything that you want to expose to QML
must exist in a QML_ELEMENT or Q_GADGET class.
callables must be either slot or Q_INVOKABLE
and variables must be property
This guide is a good introduction.

Qt5 C++ Link New Window with Qt Designer

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.

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 get ATL support into an existing Windows application

I'm building an application using Qt 5.3.1 in Visual Studio 2012. I also want to use a hardware library which requires me to add a simple ATL object to the project. This can be done by using a Visual Studio wizard. The wizard complains that my project is neither an MFC executable nor an MFC DLL with full ATL support.
My question is: How can I add this support to my application? In the project properties I configured the project to link to the ATL and use the MFC. It did not work. Both statically and dynamically.
If there's another solution in order to add a simple ATL object to the project, please let me know.
The wizard which adds ATL support works on source code of the C++ project, including both checking if the current code is already ATL project, whether the project is okay for adding ATL support to, and code modification per se.
If the wizard "does not like" something in your project it displays an error which basically means that the wizard does know how to safely modify your source code. It does not however mean that adding ATL support is impossible. And enabling an option in project settings is insufficient since source code needs some explicit initialization stuff.
The best you can do to add ATL support without thinking too much about it, is to create a new empty project that matches the project type you currently have, e.g. MFC application. Then take a snapshot of source code, then add ATL support using the wizard. Then compare changes and duplicate them on your real project. The same applies to next step of adding ATL Simple Object using Visual Studio wizard.
Some relevant links (even though the method above looks the easiest to me):
How To Add ATL Support to an MFC EXE
Adding ATL support to existing mfc application
Add automation support to MFC DLL

Compiling and Running MFC Applications with Visual Studio 2008?

I was wondering if anyone can provide a detailed explanation on how I can compile and execute a C++ MFC application in Visual Studio 2008 given that I already have a .h and .cpp file only.
I couldn't find a decent and up to date explanation anywhere and when I try to create a plain MFC project in VS08 I get a bunch of xxx.h xxxDoc.h xxxView.h files, I try and replace those files with my own .cpp and .h files but i get a ton of errors when I run it.
So just to summarize, how do I compile and run my MFC project's .h and .cpp files in Visual Studio 2008 and create an .exe?
Thanks in advance.
See here: Creating an MFC Application
This tutorial may help you as well: Introduction to MFC Applications
It depends on what you are trying to do, but deleting the files the wizard creates is not a good idea. They are there because you told it to put them there (by selecting MFC-SDI/MDI), so they are necessary for the basic initialization and such.
If you don't need the Doc/View architecture, you can create a Dialog Based application. Or, you can even use an MDI or SDI app, and not use the Doc and the View, but if you want to delete them you'll have to make sure there are no references to them. What you cannot delete is the "MyProject.cpp" (where the CWinApp derived class lays), which does the initialization of the application.
Another option is to create a Console application with MFC support. To do so, you have to select "Win32 console application" in the wizard and then check "Add headers for MFC".