Embed Qtk widget into QMainWindow application

+ Fix keyboard input focus to bind on mouse click
+ Fix missing call to `setFormat` in `MainWidget` ctor
+ Add placeholder toolbar options
This commit is contained in:
2022-03-13 18:00:00 -04:00
parent 2ed1dc800f
commit 3497b97f6e
9 changed files with 204 additions and 89 deletions

View File

@@ -22,23 +22,20 @@
MainWidget::MainWidget() : mDebugLogger(Q_NULLPTR)
{
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(4, 5);
format.setDepthBufferSize(16);
// If QTK_DEBUG is set, enable debug context
#ifdef QTK_DEBUG
format.setOption(QSurfaceFormat::DebugContext);
#endif
initializeWidget();
}
// Constructor for using this widget in QtDesigner
MainWidget::MainWidget(QWidget *parent) : QOpenGLWidget(parent), mDebugLogger(Q_NULLPTR)
{
initializeWidget();
}
MainWidget::MainWidget(const QSurfaceFormat &format)
: mDebugLogger(Q_NULLPTR)
{
setFormat(format);
resize(QSize(800, 600));
show();
setFocusPolicy(Qt::ClickFocus);
}
MainWidget::~MainWidget()
@@ -89,9 +86,9 @@ void MainWidget::initObjects()
mObject->mProgram.enableAttributeArray(0);
mObject->mProgram.setAttributeBuffer(0, GL_FLOAT, 0,
3, sizeof(mObject->vertices()[0]));
mObject->mProgram.setUniformValue("uColor", QVector3D(0.0f, 0.25f, 0.0f));
mObject->mProgram.setUniformValue("uColor", QVector3D(1.0f, 0.0f, 0.0f));
mObject->mProgram.setUniformValue("uLightColor", WHITE);
mObject->mProgram.setUniformValue("uAmbientStrength", 0.2f);
mObject->mProgram.setUniformValue("uAmbientStrength", 0.75f);
mObject->mVBO.release();
mObject->mVAO.release();
@@ -248,6 +245,7 @@ void MainWidget::messageLogged(const QOpenGLDebugMessage &msg)
void MainWidget::keyPressEvent(QKeyEvent *event)
{
if (event->isAutoRepeat()) {
// Do not repeat input while a key is held down
event->ignore();
} else {
Input::registerKeyPress(event->key());
@@ -278,6 +276,24 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *event)
* Private Helpers
******************************************************************************/
void MainWidget::initializeWidget()
{
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(4, 6);
// Set the number of samples used for glEnable(GL_MULTISAMPLING)
format.setSamples(4);
// Set the size of the depth bufer for glEnable(GL_DEPTH_TEST)
format.setDepthBufferSize(16);
// If QTK_DEBUG is set, enable debug context
#ifdef QTK_DEBUG
format.setOption(QSurfaceFormat::DebugContext);
#endif
setFormat(format);
setFocusPolicy(Qt::ClickFocus);
}
void MainWidget::printContextInformation()
{
QString glType;

View File

@@ -31,6 +31,7 @@ Q_OBJECT;
public:
// Constructors
MainWidget();
explicit MainWidget(QWidget *parent);
explicit MainWidget(const QSurfaceFormat &format);
~MainWidget() override;
@@ -57,6 +58,7 @@ protected:
private:
// Private helpers
void initializeWidget();
void printContextInformation();
void updateCameraInput();

15
src/mainwindow.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon("../resources/icon.png"));
}
MainWindow::~MainWindow()
{
delete ui;
}

24
src/mainwindow.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "main-widget_export.h"
namespace Ui {
class MainWindow;
}
class MAIN_WIDGET_EXPORT MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

104
src/mainwindow.ui Normal file
View File

@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Qtk - MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="MainWidget" name="openGLWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>775</width>
<height>550</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuTest">
<property name="title">
<string>File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionSave_2"/>
<addaction name="actionSave_as"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuView">
<property name="title">
<string>View</string>
</property>
<addaction name="actionShow_Console"/>
</widget>
<addaction name="menuTest"/>
<addaction name="menuView"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOtherTest">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>Open...</string>
</property>
</action>
<action name="actionSave_2">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionSave_as">
<property name="text">
<string>Save as...</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
<action name="actionShow_Console">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show Console</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>MainWidget</class>
<extends>QOpenGLWidget</extends>
<header>mainwidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -24,7 +24,10 @@ QMatrix4x4 Scene::mProjection;
Scene::Scene()
{
init();
mCamera.transform().setTranslation(0.0f, 0.0f, 20.0f);
mCamera.transform().setRotation(-5.0f, 0.0f, 1.0f, 0.0f);
init();
}
Scene::~Scene()
@@ -44,10 +47,6 @@ Scene::~Scene()
void Scene::init()
{
mCamera.transform().setTranslation(0.0f, 0.0f, 5.0f);
mCamera.transform().setRotation(180.0f, 0.0f, 1.0f, 0.0f);
// Initialize Phong example cube
mTestPhong = new MeshRenderer("phong", Cube());
mTestPhong->mTransform.setTranslation(3.0f, 0.0f, -2.0f);