Skip to content

QtWS: Early Bird Tickets Available!

  • 0 Votes
    4 Posts
    133 Views
    G

    For the historical record, I finally got it working. My code:

    `

    QRect screenGeometry = QGuiApplication::primaryScreen( )->geometry( ); int x13; int x23; int y13; int y23; if( screenGeometry.width( ) < width( )) { x13 = m_image->getScrollX( ) + screenGeometry.width( ) / 3.0; x23 = m_image->getScrollX( ) + 2 * screenGeometry.width( ) / 3.0; } else { x13 = m_image->getScrollX( ) + width( ) / 3.0; x23 = m_image->getScrollX( ) + 2 * width( ) / 3.0; } if( screenGeometry.height( ) < height( )) { y13 = m_image->getScrollY( ) + screenGeometry.height( ) / 3.0; y23 = m_image->getScrollY( ) + 2 * screenGeometry.height( ) / 3.0; } else { y13 = m_image->getScrollY( ) + height( ) / 3.0; y23 = m_image->getScrollY( ) + 2 * height( ) / 3.0; } QPainter painter( this ); painter.setPen( QPen( Qt::red, 4, Qt::SolidLine, Qt::FlatCap )); painter.drawLine( 0, y13, width( ), y13 ); painter.drawLine( 0, y23, width( ), y23 ); painter.drawLine( x13, 0, x13, height( )); painter.drawLine( x23, 0, x23, height( ));

    }

    `

  • 0 Votes
    7 Posts
    177 Views
    Pl45m4P

    @herocon

    Ok next attempt:

    ui->label->setPixmap(p.scaled(ui->label->size()));

    Each iteration scales the pixmap to current label size and then sets the pixmap to the label.
    By doing this, the size of the label increases with the pixmap (because of border/frame... IIRC 1px per side by default, so 2px in total).
    Then you take the new label size again in the next iteration and apply this new (by 2px) increased size to the pixmap... the label gets the pixmap, grows by about 2px in height and there you have your loop where your label and consequently the whole widget grows ;-)

    So still no bug :)
    And it's exactly the behavior you would expect from the debug output.
    (The height increases by 2 each timer interval).
    When using another layout setup, the layout/other widgets might "buffer" that, so the main app window does not grow.
    But even there, the label itself should definitely grow when you periodically set content which has the exact same size as the outter boundings of the label.
    Why though?!

    Try (not tested):

    int w = ui->label->width(); int h = ui->label->height(); ui->label->setPixmap(p.scaled(w - 2, h - 2)); // or this and set the pixmap only once ui->label->setScaledContents(true);

    Because a label with w=500, h=500 for example, can't display a pixmap with w=500, h=500. If you set a pixmap with the same size as the label itself, it will start to grow.

  • 0 Votes
    11 Posts
    1k Views
    JonBJ

    @Emrecp
    I believe @SGaist is telling you that you cannot guarantee that a widget with a stylesheet will look identical to one you construct with coded style attributes. As soon as you place a stylesheet on a widget you are (potentially) losing some styles which are inbuilt, so it may differ.

    I suggested earlier you remove the weight stuff in your example and see whether just with the font in "normal" it does or does not look identical using the same font in both code and stylesheet.

    If you are coding inside paintEvent() then, as you say, you cannot use stylesheet. You can only set the attributes on a QFont as you have done.

    I don't know whether you can go through the sources of Qt to discover just what a current style like Fusion does.

  • 0 Votes
    5 Posts
    365 Views
    jsulmJ

    @timob256 said in How include "qfont" in "qlabel" ??:

    I did not understand which command to write to make it work

    Open documentation for setFont: https://doc.qt.io/qt-6/qwidget.html#font-prop
    As you can clearly see setFont does not take a pointer to QFont, but you're trying to give it a pointer. This is basic C++ knowledge.
    Change to:

    ui->label->setFont(*font);

    Or, even better, don't declare font as pointer...

  • 0 Votes
    4 Posts
    451 Views
    S

    Hi all,

    I finally managed to find out what was the problem. The fix is the following

    @@ -26,7 +26,7 @@ void MainWindow::mousePressEvent(QMouseEvent* event) // If a QLabel contains the mouse pointer, then this is the QLabel that was selected QList<QLabel*> qLabelList = ui->videoStreamsFrame->findChildren<QLabel*>(); foreach(QLabel *l, qLabelList) { - QPoint mappedMousePosition = l->mapFromGlobal(mousePosition); + QPoint mappedMousePosition = l->mapToParent(l->mapFromGlobal(mousePosition)); if (l->geometry().contains(mappedMousePosition)) { rubberBand = new QRubberBand(QRubberBand::Rectangle, l); rubberBand->setGeometry(l->rect());

    So, the mapFromGlobal() is translating the global coordinates to widget coordinates and the geometry(), where we use in order to check if the mouse coordinates are contained in the label, has the coordinates relative to the parent widget. So, we need to map the mouse coordinates to the parent widget, hence the function mapToParent().

    Kind regards,

    Stavros

  • 0 Votes
    6 Posts
    728 Views
    JonBJ

    @BigBen

    Store them in a container yourself as you create them: QList<QLabel *> labels; for (int i = 0; i < 10; i++) { QLabel *label = new QLabel; someLayout->addWidget(label); labels.append(label); } Assign them an objectName for future reference to recall an individual one: QList<QLabel *> labels; for (int i = 0; i < 10; i++) { QLabel *label = new QLabel; label->setObjectName(QString("label_%1").arg(i)); someLayout->addWidget(label); } QLabel *label_2 = someParentWidget->findChild<QLabel *>("label_2"); Collect them all via findChildren(): QList<QLabel *> labels = someParentWidget->findChildren<QLabel *>();
  • 0 Votes
    5 Posts
    543 Views
    B

    I see. Thank you.

  • 0 Votes
    9 Posts
    2k Views
    W

    Awesome, I will check that out, thank you very much!

  • 0 Votes
    6 Posts
    1k Views
    JKSHJ

    @new-qt_user-2022 All the best! Feel free to post new questions if you'd like further help.

  • 0 Votes
    9 Posts
    908 Views
    Swati777999S

    @jsulm said in Cout in a loop for QLabel Widgets:

    @Swati777999

    Names[ii] = new QLabel(QString("Name %1").arg(ii));

    Works perfectly! Thanks.

  • 0 Votes
    5 Posts
    1k Views
    JonBJ

    @DougyDrumz2 said in How do I make a QLabel Blink?:

    The consensus is evidently to l hide and show a QLabel

    Not my "consensus :) So far as I am aware, hiding can cause different layout redraw in at least some circumstances. I retain visibility but toggle foreground color to QColor(Qt::transparent) or toggle alpha color value between 255 (opaque) and 0 (transparent) on palette QPalette::setColor(QPalette::Text, colour).

    Also, creating multiple QTimer::singleShot()s doesn't scale very well if you want to change the number of flashes or the interval between. Or of you have multiple labels to blink and you'd like them all to be "in sync". I use one regular repeating QTimer with a "countdown" and cancellation when it reaches 0.

    Up to you on both of these.

  • 0 Votes
    8 Posts
    858 Views
    mrjjM

    @Sai-Raul

    ok.
    but Qlabels only likes pixmaps.

  • 0 Votes
    2 Posts
    388 Views
    jsulmJ

    @Sai-Raul Start here: https://doc.qt.io/qt-5/videooverview.html

  • Image showing in QLabel

    Unsolved General and Desktop
    4
    0 Votes
    4 Posts
    509 Views
    Christian EhrlicherC

    @Sai-Raul said in Image showing in QLabel:

    Already tried that

    And what was the outcome? Please update your code accordingly.

  • 0 Votes
    9 Posts
    2k Views
    H

    @eyllanesc Oh I see, it's working now. Thank you!

  • 1 Votes
    10 Posts
    4k Views
    T

    @J-Hilk Thanks for the response, I'll look into that stackoverlfow thread

  • 0 Votes
    3 Posts
    2k Views
    S

    @ChrisW67
    Thanks! It work like a charm.

    For the reason to why I was using QLabel, well I'm mostly testing for stuffs. I'll switch it up later.

  • 0 Votes
    7 Posts
    942 Views
    S

    @JonB Thanks, this is new information for me. Actually the "QVTKWidget" was sufficing my problem to display point cloud data. For now it is working properly because I m using VTK 7.0. It seems post VTK 8.0, the QVTKOpenglWidget needs to be used.

  • 0 Votes
    1 Posts
    246 Views
    No one has replied
  • 0 Votes
    8 Posts
    2k Views
    Chris KawaC

    @TUStudi said:

    I actually have 6 QLabels, so I now have 6 timer and so on

    Derive a class from QLabel and add that functionality to it. Don't write the same code 6 times!

    My QLabels have the sizePolicy horizontal: preferred and vertical: fixed and a vertical length of 20.
    When a text is displayed, the QLabel expands so that the elements above the QLabel are also moved upwards and after the text disappears, they go back to their initial place.

    Fixed size policy means that widget uses its sizeHint() as the size. If you change the text the size hint also changes, so label grows/shrinks.