Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QScrollArea how to question (inactive scrollbars) [solved]
Forum Updated to NodeBB v4.3 + New Features

QScrollArea how to question (inactive scrollbars) [solved]

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krix
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        krix
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            krix
            wrote on last edited by
            #5

            Unfortunately I could not correct the code. I read more examples but I can not see the problem.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              krix
              wrote on last edited by
              #6

              After that I used the setMinimumSize() it works. Before the widget has autosizing.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved