QGLWidget image disappears after resizing window - qt

I am working on some software to display semitransparent images in a row.
It worked fine using glut functions for resizing etc. but then i decided to move to Qt for a GUI.
all glut functions are stripped and i use a QGLWidget to render my OpenGL stuff.
When i open the program, everything works as wanted but as soon as i resize the mainwindow (and thus the QGLWidget is being resized) the contents of my openGL widget disappear or appear cluttered.
Strangely though the widget is completely grey (my glclearcolor) when there is no cluttered render of my scene content.
This is how it should look like (after starting the program):
http://abload.de/img/okw0qh0.png
And this is how it looks like after resizing the mainwindow:
http://abload.de/img/errort1peu.png
(sometimes it is just grey)
my resizeGL function looks like this and gets called properly:
void prosta3dwidget::resizeGL(int width, int height)
{
if (height == 0) return; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height; //get aspect ratio
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.5f, 100.0f);
glViewport(0, 0, width, height);
update(); //redraw content
glMatrixMode(GL_MODELVIEW);
}

Okay, noticing that this must have something to do with the depth buffer (gluPerspective manual notes that depth buffer precision is affected by zNear and zFar values: https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml )
i disabled the GL_DEPTH_TEST (uncommented glEnable(GL_DEPTH_TEST)) and everything seems to work as desired. yet i dont fully understand why.
problem solved, new mistery achieved.

Related

QScroller in QGraphicsView resets viewport origin (Qt 5.7)

I have a simple subclass of QGraphicsView that I'd like to enhance with kinetic scrolling. My QGraphicsScene sceneRect is something like (origin = -12500,12500 w=25000, h=25000), so is centered on 0,0, but extends far negative and far positive. In the QGraphicsView, when I attempt:
if(clicked)
{
// Enable kinetic effects -- should work, but doesn't
QScroller::grabGesture(this->viewport(), QScroller::LeftMouseButtonGesture);
// this behaves as I expect:
// this->setDragMode(QGraphicsView::ScrollHandDrag);
}
When dragging, viewport's image truncates everything < 0,0 in Scene Coordinates, plus when I release the mouse, the viewport jumps so that the Scene 0,0 is located at the View 0,0 (upper left). When in this mode, the scroll bars work as expected. Hopefully, the picture included below will help...
Very confusing.
I've tried attaching the grab gesture to both the QGraphicsView and the QGraphicsView->viewport() and neither work as I expect...
Picture of Before, During, After kinetic drag

How to change QPushButton icon/text spacing with stylesheets?

I'm fairly new to Qt's method of stylesheets and I am looking to adjust the spacing between the icon and text on a QPushButton.
This is the gap I'm referring to: http://imageshack.us/scaled/thumb/593/4kem.png (stackoverflow won't let me post pics yet)
QPushButton {
qproperty-icon: theme_url("/button_action/add");
qproperty-iconSize: 14px;
}
Is there a parameter I can use to adjust this space? I've tried margin, padding, spacing? Perhaps there is a different selector that I can use to just grab the icon?
Actually with QPushButton you are out of luck. You can find a complete list of working options for styling push buttons in the qt documentation ( I am assuming you use qt5, but it is available for other versions as well).
What is often recommended if you want more control over the icon is to exchange your QPushButtons with QToolButtons wich are exactly the same except that they have extra features to make them compatible with toolbars -and- they have more options for icon placement. But this might not work for you because as you can see they are styled the exact same way as QPushButtons. But do look into the "toolButtonStyle" parameter which allows you to move the icon around.
Next on the list comes changing the artwork. If all you want is more space then just add the extra empty pixels to the artwork. This really hurts the perfectionist's mind but is effective.
Next on the list comes subclassing QPushButton and overriding the paint yourself. Sounds more daunting than it is. Subclassing in Qt has become a habit already and it works great. Here is a tutorial i googled in a jiffie (for 4.8 but should be about the same for 5) on the matter of subclassing. It even covers overriding the paint event.
Hope this was helpful.
At least with Qt5.5 (this may change in the future, I hope), besides overriding the paint event, which is complex and in my opinion messy for just a simple thing, there's no inherent Qt way to move that icon a few pixels here or there (small adjustments). If you remove the icon and use a stylesheet to set the background to your icon in your resource URL, you can use background-position property in the stylesheet, but unfortunately it only accepts words like center, left, etc., not pixel adjustments.
Here's how I resolved this problem. I created transparent PNGs that were slightly larger than I needed, where I had like a 4px margin around the icon itself inside the PNG. Then, I just moved the icon inside that canvas and reloaded it into the project resources file, and then mapped it again with the icon property in the designer for the QPushButton, and that worked. I now have icons that line up properly on the left with the text beside it. I can even widen the gap between icon and text.
As of Qt5.7 I don't know of any built-in functionality for the icon/text spacing.
What I usually do is give the image some more space to the right and set the icon size of the button accordingly.
Solution:
Resize your icon file to 32*16 px but leave its content as is.
Call setIconSize(QSize(32, 16)) on your button.
Result: Now there's 16 pixel empty space between its icon and text. 32 pixel is just my example, you can of course set the additional size to something that suites your style.
If you want to apply that on all your button elements simply create a tiny subclass that does it automatically in the constructor. There's really no need to override the paintEvent method for this.
Here is a workaround way to achieve that 'space between text and image':
QPushButton{
border:0px;
image: /*your url*/;
padding-top:16px;
image-position:top;
text-align:bottom;
padding-bottom:10px;
}
since qss doesn't support relative size, you need to implement resize() to change stylesheet when size changing to get a fixed space between text and image.
Here's an opportunistic approach to customize the spacing between text and icon. There's currently no regular way to change the width, so we need to add a transparent area to the right of the icon which is applied to paint device.
Firstly, add a new member variable "m_spacing"(double) and initialize it to 0.0 in constructor, as a factor of expanding the width.
Then, override the "paintEvent", "sizeHint" and "minimumSizeHint".
class CPushButton : public QPushButton {
Q_OBJECT
public:
double spaceRatio() const;
void setSpaceRatio(double ratio);
protected:
double m_spacing;
void paintEvent(QPaintEvent *event) override;
......
QSize CPushButton::sizeHint() const {
QSize sz = QPushButton::sizeHint();
int offset = iconSize().width() * m_spacing;
return QSize(sz.width() + offset, sz.height());
}
QSize CPushButton::minimumSizeHint() const {
QSize sz = QPushButton::minimumSizeHint();
int offset = iconSize().width() * m_spacing;
return QSize(sz.width() + offset, sz.height());
}
void CPushButton::paintEvent(QPaintEvent *event) {
QSize sz = iconSize();
QPixmap tmp = icon().pixmap(sz); // Get the pixmap to apply with right size
sz.rwidth() *= 1 + m_spacing; // Multiply width
QPixmap exp(sz); // Expended
exp.fill(Qt::transparent);
QPainter painter(&exp);
painter.drawPixmap(QRect(QPoint(), tmp.size()), tmp);
QStylePainter p(this); // From Qt source
QStyleOptionButton option; // From Qt source
initStyleOption(&option); // From Qt source
option.icon = QIcon(exp); // Change to real icon
option.iconSize = sz; // Change to real size
p.drawControl(QStyle::CE_PushButton, option); // From Qt source
Q_UNUSED(event)
}
Now you can create a CPushButton instance and set the spacingRatio to 0.1 or other positive real. Visually, it does increase the spacing between text and icon.
The simplest way to go for it is to create your own class inheriting from the QPushButton and override the PaintEvent and then manually place the pixmap of the icon where ever you want.
Best way to solve this problem is adding some blank space in the beginning of QPushButton text. it works well for me!
See here

QMainWindow::showMaximized() Doesn't Update Size

I'm trying to create a QMainWindow to encapsulate a QGraphicsView that I'm putting in it. I want it to start out maximized, so I do this:
QMainWindow *mainWindow = new QMainWindow();
mainWindow->setWindowState(Qt::WindowMaximized);
mainWindow->show();
qDebug() << mainWindow->size();
Which says my maximized window is 200x100, which is obviously incorrect.
Am I missing some sort of update function? I don't get why it wouldn't update the size. I've also tried using showMaximized() with the same result.
EDIT
My end-goal is to use the QMainWindow as a container for a QGraphicsView containing a QGraphicsScene. On top of all this, I want to have a QWebView at 50% width and 100% height, centered over everything.
So, I need the width and height in order to get all the coordinates and sizes correct.
Well, the effect of setWindowState() is not immediate, it gets executed asynchronously. When the window state changes, the widget receives a changeEvent(), so you should reimplement this or resizeEvent() to get the width() and height() after the maximization takes place.

Resize window to fit content

I have a QGLWidget, which I want to resize to a given resolution (bigger than the containing window).
My intention is, that the window expands until the widget fits inside, but can't find a way to do it.
I tried several commands after resizing the QGLWidget to make it work without success.
I will list the results here:
do nothing else: The Widget overlaps the whole window. Eventually it will be resized to fit back into the smaller window
mainWindow.adjustSize(): The widget gets resized to (0, 0)
mainWindow.resize(mainWindow.sizeHint()): see above
mainWindow.resize(mainWindow.minimumSizeHint()): see above
I also read in this thread, that before doing the mainWindow resize I the event loop needs to be run to recalculate the new sizes, so I inserted QCoreApplication::processEvents to do so, without any visible effect.
So how do I resize the window via the widget?
Edit
The GLWidget is not the only widget of the window.
It is embedded in splitter together with a group box.
http://qt-project.org/doc/qt-4.8/qwidget.html#sizePolicy-prop
http://qt-project.org/doc/qt-4.8/qsizepolicy.html#Policy-enum
http://qt-project.org/doc/qt-4.8/qwidget.html#setFixedSize
So assuming that you have your QGLWidget nested inside your QMainWindow as the central widget, you need to set the size policy of your QGLWidget.
For example:
QGLWidget * glw; // in your header for QMainWindow
...
// In your constructor for QMainWindow
glw = new QGLWidget;
this->setCentralWidget(glw);
glw->setFixedSize(500, 500);
this->adjustSize();
Hope that helps.
I have an app that needed to be very similar to your requirements, so I'll post my solution here. An image covering the window which is freely expandable and shrinkable, and can be changed to the original size, and remain expandable / shrinkable after that.
I used a QLabel widget to display the image, but it should work with other widget types too. I created the widget with an initial size and the QSizePolicy::Ignored.
label->resize (w, h); // initial size
label->setSizePolicy (QSizePolicy::Ignored, QSizePolicy::Ignored);
The label widget was in a QVBoxLayout with a few buttons in the window, but this may work with other layout types too.
The window and image widget can be resized to the image's original size with this code:
label->resize (w, h); // change to original size
label->setMinimumSize (w, h); // prevent it from collapsing to zero immediately
window->adjustSize (); // resize the window
label->setMinimumSize (0, 0); // allow shrinking afterwards

Optimized Line drawing in QT

I am new to QT. i am working on the Graphics.
i am using QWidget for drawing graphics(For drawing graphics in QWidget paint event). i need to draw background and foreground graphics. Background is fixed graphics. foregrounds i am drawing lines.
Each 100 millisecond i need to draw 20points. This drawing time is 8 sec. Total i need to draw 1600 points (total points represents the contentious line).
i am using QTimer to invoke this drawing in each 100ms. first few drawing drawn very fast. in the middle of the drawing it's become slow.
the problem is i need to draw all the foreground and background in each 100ms.
Please help me to fix the problem. if any one have sample code please provide. Thanks in advance.
Is there any way to draw only partial area ie. only particular modified region of the graphics?
QPainter-drawing can be very slow without hardware support. Using QGraphicsView won't help if all lines are visible, since it internally uses QPainter anyway.
If you just have to draw 20 new points (or lines) per update and per update background gets cleared so you have to render everything again, there are few things you could try:
1) Disable background autofill. See: QWidget::autoFillBackground
Add something like this to your widget init:
setAutoFillBackground(false);
setAttribute(Qt::WA_OpaquePaintEvent, true);
setAttribute(Qt::WA_NoSystemBackground, true);
Now on the first update render background and first lines. For next updates just skip rendering background and render only new lines.
2) Use double buffering. For example, create QImage of the size of your widget.
.h
private:
QImage m_targetImage;
.cpp
// constructor
m_targetImage = QImage(width(), height(), QImage::Format_ARGB32);
// paint event
// draw to image
QPainter p;
p.begin(&m_targetImage);
static bool firstUpdate = true;
if (firstUpdate)
{
// draw background)
p.drawImage(...);
firstUpdate = false;
}
// draw latest lines
p.drawLines(....);
p.end();
// draw image in widget paint
QPainter painter;
painter.begin(this);
painter.drawImage(0, 0, m_targetImage);
painter.end();
3) Use QGLWidget if possible. Inherit your widget from QGLWidget instead of QWidget. This method doesn't work on all platforms and speed increase might not be enough. Also using OpenGL brings all kind of new problems.

Resources