QScrollArea how to question (inactive scrollbars) [solved]
-
I have a class named avl_widget, the class can draw binary tree data structures. I want to make it to be scrollable. In the example I use only a QLabel.
Header:
@#ifndef ALV_DRAW_H
#define ALV_DRAW_H
#include <QtGui>class AVL_draw : public QWidget
{
Q_OBJECT
public:
explicit AVL_draw(QWidget parent = 0);
protected:
void paintEvent(QPaintEvent);
QLabel l1;
};#endif // ALV_DRAW_H@
Source:
@#include "avl_draw.h"
AVL_draw::AVL_draw(QWidget *parent) :
QWidget(parent)
{
l1.setParent(this);
l1.setText("X");
l1.move(320, 240);
show();
}void AVL_draw::paintEvent(QPaintEvent*)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::white);
painter.setBrush(QBrush(Qt::white));
painter.drawRect(0,0, this->width(), this->height());
}@The parent widget has a layout:
@#ifndef AVL_WIDGET_H
#define AVL_WIDGET_H
#include "avl_draw.h"class AVL_widget : public QWidget
{
Q_OBJECT
public:
explicit AVL_widget(QWidget* parent = 0);
protected:
QScrollArea draw_scrollarea;
QVBoxLayout main;
QGridLayout buttons;
QGridLayout buttons_bottom;
AVL_draw draw;
};#endif // AVL_WIDGET_H
@Source:
@#include "avl_widget.h"
AVL_widget::AVL_widget(QWidget *parent) :
QWidget(parent)
{
setLayout(&main);
main.addLayout(&buttons);//main.addWidget(&draw); draw_scrollarea.setGeometry(draw.geometry()); draw_scrollarea.setWidget(&draw); draw_scrollarea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); draw_scrollarea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); draw_scrollarea.setWidgetResizable(true); main.addWidget(&draw_scrollarea); main.addLayout(&buttons_bottom);
}
@The scollbars are disabled, but the label is out of range. What is the problem?
-
Hi,
@l1.move(320, 240);@
You just put it at that fixed position and never update its position or size. Since you don't put it in layout, it's your job to do it. But since your label only contains X and you are painting the widget yourself, why not draw the X yourself at the right place ?
On a side note, you should use pointers to your widgets/layouts
-
Hi,
Which function can update the position and size?
I accept your hint, that paint the X to the correct place, I only used QLabel to simplify the code. If I paint graphes with painter to widget, I can not scrolling it too.
What is the reason that I should use pointers to widgets/layouts?Many thanks SGaist!
-
The resize function for example.
Have a look at the "Basic drawing example" it should help you get started.
As for why you should rather do heap allocation see "this ":http://qt-project.org/forums/viewthread/2962 thread. More specifically the answer from peppe from the 4th january 2011