Settings errors in Qt project with OpenGL - c++

In the project that use OpenGL in Qt I use in protected method initializeGL() the statement
qglClearColor(qtPurple.dark());
Follows errors occurs in the building project:
‘qtPurple’ was not declared in this scope
‘qglClearColor’ was not declared in this scope
The files that is included is:
#include <QtGui>
#include <QtOpenGL>
#include <QtGui/QColor>
In the .pro file is present
QT += core gui, opengl
Where are the mistakes that cause these errors?

QGLClearColor is a non-static member of QGLWidget. So first you must include <QGLWidget> to your widget header file and inherit your widget from QGLWidget. Then you will be able to call it in methods of your widget. You get was not declared in this scope error because qglClearColor is in QGLWidget scope.
Alternatively, you can call it as regular method of your widget object.
And what is qtPurple? It seems that it's not a part of Qt.

Add #include <QtOpenGL/QGLWidget> in your head file.And Your class should inherit QGLWidget.
It seems that you have not declared the variable qtPurple,so check
your head file,if not exist,just declare it[like this:QColor qtPurple;].

Related

wxWidgets - Splitting project into multiple files

I am very new to C++, I'm making this music application using wxWidgets, this is my first project in C++. As of now, I have 4 files, app.hpp and app.cpp which has a class that inherits from wxApp that launches the application, and frame.hpp and frame.cpp which holds the base frame and panel, and all the widgets, and their appropriate functions. I want to move all the functions to a separate file, but I get some errors like there is this function in frame.cpp
void Frame::ClearPlaylist(wxCommandEvent& event)
{
mediaCtrl->Stop();
playlistBox->Clear();
}
I tried moving it in another file called command.cpp and created a new class called command and prefixed all functions to Command:: .... and somethings like the playlistBox here, is a widget which I want in frame.cpp only, as it is a widget, so I did #include frame.hpp and prefixed it with Frame::playlistBox, but that gave a error saying invalid use of non static data member. So do I have to make everything in frame.hpp a static object? Or if anyone has a better solution for organizing a project like this please do share.

Load OpenGL function pointers using glad withing a QOpenGLWidget

So I am reading up on some OpenGL and I want to use the QOpenGLWidget for drawing to maybe create some other helpful UI elements later. I am using glad for resolving the function pointers to OpenGL but I have no idea how to use Qt's getProcAddress function!
Inside my QOpenGLWidget subclass' initializeGL() function I have tried:
if(gladLoadGLloader((GLADloadproc) currentContext()->getProcAddress) {}
but that did not work out since Qt's function is overloaded. When I use
if(gladLoadGL()) {}
it doesn't work either. My includes are:
#include <glad\glad.h>
#include "OpenGLViewport.h"
#include <QDebug>
#include <QOpenGLContext>
I have searched Mr. Google and I've had a diligent look through the Qt documentation and found nothing. I want to use GLAD just so my rendering code is not bound to Qt too tightly, in case I want to switch later.
EDIT: I am aiming to use the noninstanced OpenGL functions with Qt (though the documentation recommends otherwise if I recall correctly). Because then I'd be able to seemlessly switch to GLFW for providing a window etc.
Moved solution from question to answer:
ANSWER: So it turns out I just had some things mixed up, this is how I got it to work, in case anyone has the same problem:
add glad.c in your project
add the necessary headers to your include directory
the .cpp file of your QOpenGLWidget subclass should have following components:
// Subclass.cpp
#include <glad/glad.h>
// important, subclass header file after glad!!
// otherwise glad won't "hook"
#include "Subclass.h"
void Subclass::initializeGL()
{
if(gladLoadGL()) initialized = true; // initialized is a class member here
else; // handle errors
}
void Subclass::paintGL()
{
if(initialized); // render here
}

Replacing widget with custom template widget in Qt Creator Designer

In Qt Creator UI Designer it is possible to replace a widget with its subclass. I have created a template subclass of QComboBox:
template <typename T>
class MappedComboBox : public QComboBox
{
// ...
};
And I have successfully managed to replace QComboBox with MappedComboBox<int>. However replacing QComboBox widget with, for instance, MappedComboBox<QSerialPort::BaudRate> fails due to dependency errors while building like
'QSerialPort' was not declared in this scope.
Of course one way to get rid of them is to include QSerialPort in mappedcombobox.h however that's not a very elegant way. Can I somehow tell Qt Designer to include additional files while generating UI?
In the UI designer there is no way to include an extra header. A better workaround is to include it in the cpp file of the designer class before including the generated header. That's better than including in the MappedComboBox which has no business with that header.
In mainwindow.cpp:
#include "mainwindow.h"
#include <QSerialPort>
#include "ui_mainwindow.h"

Why Qt default project use separate header files for manwindow.cpp?

I just created a Qt default project with a Qt designer form.
The class MainWindow is declared in a mainwindow.h and then included in mainwindows.cpp.
Why is it done this way ? Why not a declaration of this form directly in mainwindows.cpp ?:
class MainWindow
{
...
}
What is the proper way to add my code ? For example, a button that trigger a method.
In C++ you typically put class definitions into header files (.h), and method implementations in source files (.cpp). That allows clients of the class to use the class without having to see the implementation of each function. That also means that when adding a method, you'll typically have to make two changes: add the method to the class definition (in the header) and then add the method's implementation to the .CPP file.
In header file:
class MainWindow
{
void SomeMethod();
};
In source file:
void MainWindow::SomeMethod()
{
// Your code here.
}
The definition of MainWindow class is needed in another file, where an instance of it is constructed in the main function and then shown. That's why the class needs to be defined in a header file.
There are a number of ways to add your own code: for the button you described you could create in entirely in the QtCreator UI, or you could create it "programmatically" in the MainWindow constructor.

Ui namespace in Qt widget projects

I have a Qt widget project called "SeasonCreator" and am wondering about the structure of a Qt widget project.
The ui_seasoncreator.h file is hidden, but when you look into it, it defines a namespace called "Ui" and in it is a declaration of a class SeasonCreator, which inherits from Ui_SeasonCreator, which is the c.
In the seasoncreator.h file, there is also a namespace called "Ui" also with a declaration of a class called "SeasonCreator". When I click to find its original declaration it leads me to the declaration of Ui::SeasonCreator in the ui_seasoncreator.h file.
What is the purpose for these namespace and classes? Do the two definitions relate?
Do any of these Ui::SeasonCreator declarations have anything to do with the custom SeasonCreator class in seasoncreator.h?
What is the purpose for these namespace and classes?
The code in ui_XXX.h is code that is automatically generated for you when you use designer to create a form. It's put into the Ui namespace.
Do the two definitions relate?
I'm assuming you're referring to something that looks like this:
namespace Ui { class SeasonCreator; }
which is a forward declaration of the class that is part of the generated code that gives your class access to the widgets in your custom form.
Do any of these Ui::SeasonCreator declarations have anything to do with the custom SeasonCreator class in seasoncreator.h?
It's what links your custom code together with the generated code. You should probably have a member variable of your custom class called Ui::SeasonCreator *ui, which is the object you use to access the generated Ui classes.