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. Destination of sizeHint()
Qt 6.11 is out! See what's new in the release blog

Destination of sizeHint()

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 860 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.
  • W Offline
    W Offline
    wf778899
    wrote on last edited by
    #1

    Hi to all! I have tryed to use sizeHint() only for understanding how it work and wrote a simple code (see below). As i understood documentation, this function is used by the Layout classes for change size of main window according to size of child widged, added in layer. I have implemented sizeHint() in class "my". In this class i draw a red rectangle 100x100.

    @
    #include "my.h"
    #include <QPainter>
    #include <QSize>

    my::my(QWidget *parent):QWidget(parent) {

    }

    void my::paintEvent(QPaintEvent *) {
    painter = new QPainter(this);
    painter->setBrush(Qt::red);
    painter->drawRect(0, 0, 100, 100);
    }

    QSize my::sizeHint() {
    return QSize(100, 100);
    }
    @

    In code below i add this widget in a layer of main widget.

    @
    CharList::CharList(QWidget *parent) : QWidget(parent) {
    test = new my; //child widget.
    QVBoxLayout *lay1 = new QVBoxLayout;
    lay1->addWidget(test);
    setLayout(lay1);
    }
    @

    As i understood correctly, QVBoxLayout shoud resize main widget (CharList) according to size my-wiget. But it's don't work. When i run the programm, the window whith the minimum size is appear... Can the anybody help me?

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jan-Willem
      wrote on last edited by
      #2

      Hi,

      Take a look at the Basic Drawing Example.
      I'm not sure if it will solve your issue, but @ painter = new QPainter(this);@

      has to be @QPainter painter(this);@

      And perhaps you should start with @painter.save()@

      and end with

      @painter.restore()@

      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