PDA

View Full Version : OpenGL Interface



Atomic_Sheep
11th October 2015, 08:47
Hi Guys,

I'm trying to make an OpenGL window without the native "title bar", minimise, maximise and resize buttons as well as the border that you get when you run a normal application.

I tried:


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//w.show();
w.showFullScreen();

return a.exec();
}

Having added a widget to my window as per below attachment but I get a white border (you can't really see the white border in full screen mode, but basically you see a black screen with a white border of around 10-15 pixels in size) if I run the program in full screen mode which I also don't want. So basically I want to be able to create my own interface for example:

http://i.ytimg.com/vi/hFoHYmWWYMI/maxresdefault.jpg

Which can run in full screen or not full screen mode.

EDIT: I've added another example for the black screen with border, you can see a bit off my second monitor and then the black screen with the white border on the main screen.

anda_skoa
11th October 2015, 11:20
Have you set the margins of your outermost layout to 0?

Cheers,
_

Atomic_Sheep
11th October 2015, 13:02
Ah, I think you're probably right. I spent an hour trying to figure this out by my programming isn't up to scratch.

I found the margin size in the xml


<layoutdefault spacing="6" margin="11"/>

I know I'm supposed to use:


layout()->setContentsMargin(0,0,0,0);

But I'm not sure where I'm supposed to use that in my code:

main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//w.show();
w.setContentsMargins(0,0,0,0); ///////////////////////////////Having this here doesn't work
w.showFullScreen();

return a.exec();
}


mainwindow.c


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}


glwidget.cpp


#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
}

void GLWidget::initializeG()
{

}

void GLWidget::paintGL()
{

}

void GLWidget::resizeGL(int w, int h)
{

}

anda_skoa
11th October 2015, 13:17
Well, since you say you found the margins in XML, I assume you mean a .ui file as generated by Qt Designer.
So the easiest way is to change the margins in designer.

Or, if the GLWidget is the one that should be full screen, don't embed it into another and just create an instance of GLWidget in main().

Cheers,
_

Atomic_Sheep
12th October 2015, 01:49
Well, since you say you found the margins in XML, I assume you mean a .ui file as generated by Qt Designer.
So the easiest way is to change the margins in designer.

Or, if the GLWidget is the one that should be full screen, don't embed it into another and just create an instance of GLWidget in main().

Cheers,
_

Yes I was referring to the ui file generated by Qt Designer.

I think I know which setting I need to change in designer although you can see it showing 9 x 9 rather 11 x 11 as shown in the .ui file, but I can't modify it since it's grayed out for some reason.

anda_skoa
12th October 2015, 10:23
No, that's the widget's geometry.
Is is disabled because it is in a layout and the layout controls the geometry.

Change the margins of the layout.

Any specific reason you need a parent widget at all?

Cheers,
_

Atomic_Sheep
13th October 2015, 13:05
Got the result I was looking for in terms of borders thanks:


QApplication a(argc, argv);

GLWidget glwidget;
MainWindow w;

w.setCentralWidget(&glwidget);
QHBoxLayout mainLayout;
mainLayout.setMargin(0);

w.show();
//w.showFullScreen();

return a.exec();


No, that's the widget's geometry.
Is is disabled because it is in a layout and the layout controls the geometry.


Ah that makes sense.



Any specific reason you need a parent widget at all?


What do you mean?

anda_skoa
13th October 2015, 14:07
What do you mean?
I mean why put the GL widget inside another widget if that widget is not visible at all?

Cheers,
_

Atomic_Sheep
14th October 2015, 08:45
I mean why put the GL widget inside another widget if that widget is not visible at all?

Cheers,
_

If I understood you correctly, I've re-made my code to now have just one class as per below:

windows2_0.pro

#-------------------------------------------------
#
# Project created by QtCreator 2015-10-14T16:48:53
#
#-------------------------------------------------

QT += core opengl

QT -= gui

TARGET = Windows2_0
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
glwidget.cpp

HEADERS += \
glwidget.h


glwidget.h

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>
#include <QDebug>

class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);

void initializeGL();
void paintGL();
void resizeGL(int w, int h);

};

#endif // GLWIDGET_H

glwidget.cpp

#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
}

void GLWidget::initializeGL()
{
qDebug() << "2";
}

void GLWidget::paintGL()
{
qDebug() << "3";

glClear(GL_COLOR_ARRAY_STRIDE | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

/*
glBegin(GL_TRIANGLES);
glVertex3f(0,0, 1.0, 0.0);
glVertex3f(-1,0, -1.0, 0.0);
glVertex3f(1,0, -1.0, 0.0);
glEnd();
*/
}

void GLWidget::resizeGL(int w, int h)
{
qDebug() << "1";
}


main.cpp

#include <QtCore/QCoreApplication>
#include "glwidget.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

GLWidget glWidget;

glWidget.show();

return a.exec();
}


However, now I'm getting a linker error:


debug/moc_glwidget.o:moc_glwidget.cpp:(.rdata$_ZTV8GLWid get[vtable for GLWidget]+0x38): undefined reference to `QWidget::devType() const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x3c): undefined reference to `QWidget::setVisible(bool)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x40): undefined reference to `QWidget::sizeHint() const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x44): undefined reference to `QWidget::minimumSizeHint() const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x48): undefined reference to `QWidget::heightForWidth(int) const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x4c): undefined reference to `QWidget::getDC() const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x50): undefined reference to `QWidget::releaseDC(HDC__*) const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x58): undefined reference to `QWidget::mousePressEvent(QMouseEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x5c): undefined reference to `QWidget::mouseReleaseEvent(QMouseEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x60): undefined reference to `QWidget::mouseDoubleClickEvent(QMouseEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x64): undefined reference to `QWidget::mouseMoveEvent(QMouseEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x68): undefined reference to `QWidget::wheelEvent(QWheelEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x6c): undefined reference to `QWidget::keyPressEvent(QKeyEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x70): undefined reference to `QWidget::keyReleaseEvent(QKeyEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x74): undefined reference to `QWidget::focusInEvent(QFocusEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x78): undefined reference to `QWidget::focusOutEvent(QFocusEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x7c): undefined reference to `QWidget::enterEvent(QEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x80): undefined reference to `QWidget::leaveEvent(QEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x88): undefined reference to `QWidget::moveEvent(QMoveEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x90): undefined reference to `QWidget::closeEvent(QCloseEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x94): undefined reference to `QWidget::contextMenuEvent(QContextMenuEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x98): undefined reference to `QWidget::tabletEvent(QTabletEvent*)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0xd8): undefined reference to `QWidget::paletteChange(QPalette const&)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0xdc): undefined reference to `QWidget::fontChange(QFont const&)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0xe0): undefined reference to `QWidget::windowActivationChange(bool)'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0xe4): undefined reference to `QWidget::languageChange()'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x120): undefined reference to `non-virtual thunk to QWidget::devType() const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x128): undefined reference to `non-virtual thunk to QWidget::getDC() const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x12c): undefined reference to `non-virtual thunk to QWidget::releaseDC(HDC__*) const'
debug/moc_glwidget.o:moc_glwidget.cpp: (.rdata$_ZTV8GLWidget[vtable for GLWidget]+0x130): undefined reference to `non-virtual thunk to QWidget::metric(QPaintDevice::PaintDeviceMetric) const'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\Windows2_0.exe] Error 1
mingw32-make: *** [debug] Error 2
The process "D:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project Windows2_0 (target: Desktop)
When executing build step 'Make'

So I obviously need something like widgets or gui in my .PRO file and something like <QMainWindow> in my glwidget.h and inherit from QMainWindow/QWidget, but haven't figured out exactly what. Hopefully you could point me in right direction.

I've never tried to understand linker errors before, so this is new territory for me.

I'm aware that I don't have a destructor at this point in time, still trying to work that out.

anda_skoa
14th October 2015, 12:01
QT -= gui


How do you expect this to work?
QtGui is a dependency of QtWidgets.



So I obviously need something like widgets or gui in my .PRO file

Yes, of course.
I assumed you already had that. How did you get the window to show up if you can't even build the program?



and something like <QMainWindow> in my glwidget.h

Why would you need that?



I'm aware that I don't have a destructor at this point in time, still trying to work that out.

If you don't need to do any special destruction handling you won't need a destructor.

Cheers,
_

Atomic_Sheep
15th October 2015, 13:30
I assumed you already had that. How did you get the window to show up if you can't even build the program?


I started a new program as a console application, that's why the .PRO file was wrong, totally forgot to ammend it in the new program (have been away from programming for a while), thanks for reminding me. The V1.0 had a correct .PRO file I just didn't post it in my initial posts.



Why would you need that?


Still trying to figure it out how to get a glwidget to show up, but at least I got this code to work:

WidgetTest.pro

QT += core gui

TARGET = WidgetTest

TEMPLATE = app

SOURCES += main.cpp \
mywidget.cpp

HEADERS += \
mywidget.h

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();

protected:
virtual void mousePressEvent( QMouseEvent *e );
virtual void mouseMoveEvent( QMouseEvent *e );
virtual void mouseReleaseEvent( QMouseEvent *e );

private:
bool _mousePressed;
QPoint _mousePosition;
};

#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"

MyWidget::MyWidget(QWidget *parent) : QWidget(parent), _mousePressed(false), _mousePosition(QPoint())
{
setWindowFlags( Qt::FramelessWindowHint );
}

void MyWidget::mousePressEvent( QMouseEvent *e )
{
/*
if ( e->button() == Qt::LeftButton ) {
_mousePressed = true;
_mousePosition = e->pos();
}*/
}

void MyWidget::mouseMoveEvent( QMouseEvent *e )
{
/*if ( _mousePressed ) {
move( mapToParent( e->pos() - _mousePosition ) );
}*/
}

void MyWidget::mouseReleaseEvent( QMouseEvent *e )
{
/*if ( e->button() == Qt::LeftButton ) {
_mousePressed = false;
_mousePosition = QPoint();
}
*/
}

MyWidget::~MyWidget()
{
// delete stuff.
}


main.cpp

#include <QtGui/QApplication>
#include "mywidget.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

MyWidget myWidget;
myWidget.show();

return a.exec();
}


My problem was that I created the new project as a console app and it used QCoreApplication rather than QApplication, so the main.cpp had the incorrect statement:


QCoreApplication a(argc, argv);

Now I get a grey window as I wanted :). Now I just need to figure out how to get my opengl window to show up.

I'm getting this error that wysota pointed out in this thread:

http://www.qtcentre.org/archive/index.php/t-45162.html

If I create a borderless opengl window, for some reason it doesn't show up, there is an application running, just nothing showing the moment I have:


GLWidget::GLWidget(QGLWidget *parent) : QGLWidget(parent)
{
this->setWindowFlags(Qt::FramelessWindowHint);
}


So trying to figure out how to use setMask. Hmm! Kinda funny, started with a bordered QMainWindow with an openGl widget in the middle and now that I just have glWidget, it still pops up in a window... one less class but still... strange...

Which makes me wonder, why does the grey box for the simple MyWidget pop up?