Skip to content

General and Desktop

This is where all the desktop OS and general Qt questions belong.
83.4k Topics 456.4k Posts
  • Please assist the non-expert.

    Unsolved
    7
    0 Votes
    7 Posts
    494 Views
    B
    @Ronel_qtmaster , Thanks. I think they are and I'll check on that as I head over to a Gnu Radio forum.
  • Qt6 comparing "\\" to QChar not working anymore

    Unsolved
    3
    0 Votes
    3 Posts
    306 Views
    C
    @Christian-Ehrlicher Thanks! That solved it
  • Auto-scrolling on dragging an item in QGraphicsScene

    Solved
    4
    0 Votes
    4 Posts
    605 Views
    R
    @Robert-Hairgrove It seems that the scroll bars were sometimes lagging behind after moving the objects; the objects were not really clipped, but the view was not completely scrolled which made it look like they were clipped. Here is my current changed version of diagramscene.h and diagramscene.cpp including additional headers which needed #include directives: diagramscene.h: /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef DIAGRAMSCENE_H #define DIAGRAMSCENE_H #include "diagramitem.h" #include "diagramtextitem.h" #include <QGraphicsScene> QT_BEGIN_NAMESPACE class QGraphicsSceneMouseEvent; class QMenu; class QPointF; class QGraphicsLineItem; class QFont; class QGraphicsTextItem; class QColor; QT_END_NAMESPACE #define DIAGRAMSCENE_BORDER_WIDTH 10 //! [0] class DiagramScene : public QGraphicsScene { Q_OBJECT public: enum Mode { InsertItem, InsertLine, InsertText, MoveItem }; explicit DiagramScene(QMenu *itemMenu, QObject *parent = nullptr); QFont font() const { return myFont; } QColor textColor() const { return myTextColor; } QColor itemColor() const { return myItemColor; } QColor lineColor() const { return myLineColor; } void setLineColor(const QColor &color); void setTextColor(const QColor &color); void setItemColor(const QColor &color); void setFont(const QFont &font); public slots: void setMode(Mode mode); void setItemType(DiagramItem::DiagramType type); void editorLostFocus(DiagramTextItem *item); signals: void itemInserted(DiagramItem *item); void textInserted(QGraphicsTextItem *item); void itemSelected(QGraphicsItem *item); protected: void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override; void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override; private: bool isItemChange(int type) const; // Added:----- void adjustDragPositions(QGraphicsView *pv, qreal border = DIAGRAMSCENE_BORDER_WIDTH); //------------ DiagramItem::DiagramType myItemType; QMenu *myItemMenu; Mode myMode; bool leftButtonDown; QPointF startPoint; QGraphicsLineItem *line; QFont myFont; DiagramTextItem *textItem; QColor myTextColor; QColor myItemColor; QColor myLineColor; }; //! [0] #endif // DIAGRAMSCENE_H diagramscene.cpp: /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "diagramscene.h" #include "arrow.h" #include <QApplication> #include <QGraphicsSceneMouseEvent> #include <QGraphicsItem> #include <QGraphicsView> #include <QScrollBar> #include <QTextCursor> //! [0] DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent) : QGraphicsScene(parent) { myItemMenu = itemMenu; myMode = MoveItem; myItemType = DiagramItem::Step; line = nullptr; textItem = nullptr; myItemColor = Qt::white; myTextColor = Qt::black; myLineColor = Qt::black; } //! [0] //! [1] void DiagramScene::setLineColor(const QColor &color) { myLineColor = color; if (isItemChange(Arrow::Type)) { Arrow *item = qgraphicsitem_cast<Arrow *>(selectedItems().first()); item->setColor(myLineColor); update(); } } //! [1] //! [2] void DiagramScene::setTextColor(const QColor &color) { myTextColor = color; if (isItemChange(DiagramTextItem::Type)) { DiagramTextItem *item = qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first()); item->setDefaultTextColor(myTextColor); } } //! [2] //! [3] void DiagramScene::setItemColor(const QColor &color) { myItemColor = color; if (isItemChange(DiagramItem::Type)) { DiagramItem *item = qgraphicsitem_cast<DiagramItem *>(selectedItems().first()); item->setBrush(myItemColor); } } //! [3] //! [4] void DiagramScene::setFont(const QFont &font) { myFont = font; if (isItemChange(DiagramTextItem::Type)) { QGraphicsTextItem *item = qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first()); //At this point the selection can change so the first selected item might not be a DiagramTextItem if (item) item->setFont(myFont); } } //! [4] void DiagramScene::setMode(Mode mode) { myMode = mode; } void DiagramScene::setItemType(DiagramItem::DiagramType type) { myItemType = type; } //! [5] void DiagramScene::editorLostFocus(DiagramTextItem *item) { QTextCursor cursor = item->textCursor(); cursor.clearSelection(); item->setTextCursor(cursor); if (item->toPlainText().isEmpty()) { removeItem(item); item->deleteLater(); } } //! [5] //! [6] void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) { if (mouseEvent->button() != Qt::LeftButton) return; DiagramItem *item; switch (myMode) { case InsertItem: item = new DiagramItem(myItemType, myItemMenu); item->setBrush(myItemColor); addItem(item); item->setPos(mouseEvent->scenePos()); emit itemInserted(item); break; //! [6] //! [7] case InsertLine: line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(), mouseEvent->scenePos())); line->setPen(QPen(myLineColor, 2)); addItem(line); break; //! [7] //! [8] case InsertText: textItem = new DiagramTextItem(); textItem->setFont(myFont); textItem->setTextInteractionFlags(Qt::TextEditorInteraction); textItem->setZValue(1000.0); connect(textItem, &DiagramTextItem::lostFocus, this, &DiagramScene::editorLostFocus); connect(textItem, &DiagramTextItem::selectedChange, this, &DiagramScene::itemSelected); addItem(textItem); textItem->setDefaultTextColor(myTextColor); textItem->setPos(mouseEvent->scenePos()); emit textInserted(textItem); //! [8] //! [9] default: ; } QGraphicsScene::mousePressEvent(mouseEvent); } //! [9] //! [10] void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) { static bool IN_EVENT = false; // Find the current view: QGraphicsView* pv = nullptr; if (mouseEvent->widget()) pv = qobject_cast<QGraphicsView *>(mouseEvent->widget()->parentWidget()); if (!IN_EVENT) { if (myMode == InsertLine && line != nullptr) { QLineF newLine(line->line().p1(), mouseEvent->scenePos()); line->setLine(newLine); } else if (myMode == MoveItem) { //-------------------------------------- if (pv) { IN_EVENT = true; QGraphicsScene::mouseMoveEvent(mouseEvent); adjustDragPositions(pv); IN_EVENT = false; } //-------------------------------------- } } } //! [10] //! [11] void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) { if (line != nullptr && myMode == InsertLine) { QList<QGraphicsItem *> startItems = items(line->line().p1()); if (startItems.count() && startItems.first() == line) startItems.removeFirst(); QList<QGraphicsItem *> endItems = items(line->line().p2()); if (endItems.count() && endItems.first() == line) endItems.removeFirst(); removeItem(line); delete line; //! [11] //! [12] if (startItems.count() > 0 && endItems.count() > 0 && startItems.first()->type() == DiagramItem::Type && endItems.first()->type() == DiagramItem::Type && startItems.first() != endItems.first()) { DiagramItem *startItem = qgraphicsitem_cast<DiagramItem *>(startItems.first()); DiagramItem *endItem = qgraphicsitem_cast<DiagramItem *>(endItems.first()); Arrow *arrow = new Arrow(startItem, endItem); arrow->setColor(myLineColor); startItem->addArrow(arrow); endItem->addArrow(arrow); arrow->setZValue(-1000.0); addItem(arrow); arrow->updatePosition(); } } //! [12] //! [13] line = nullptr; QGraphicsScene::mouseReleaseEvent(mouseEvent); } //! [13] //! [14] bool DiagramScene::isItemChange(int type) const { const QList<QGraphicsItem *> items = selectedItems(); const auto cb = [type](const QGraphicsItem *item) { return item->type() == type; }; return std::find_if(items.begin(), items.end(), cb) != items.end(); } //! [14] void DiagramScene::adjustDragPositions(QGraphicsView *pv, qreal border) { Q_ASSERT_X(pv, __func__, "Pointer to view was NULL."); if (border < DIAGRAMSCENE_BORDER_WIDTH) border = DIAGRAMSCENE_BORDER_WIDTH; // Get the selected items: QList<QGraphicsItem*> selected = selectedItems(); if (selected.isEmpty()) return; QGraphicsItemGroup * gp = createItemGroup(selected); if (gp) { QRectF br = gp->boundingRect(); qreal diffX = 0; qreal diffY = 0; // check left border: if (br.x() < border) { diffX = border - br.x(); } // check right border: if (br.x()+br.width() > this->width() - border) { Q_ASSERT_X(diffX == 0, __func__, "diffX was already set for left border " "adjustment."); diffX = -((br.x() + br.width()) - (width() - border)); } // check top border: if (br.y() < border) { diffY = border - br.y(); } // check bottom border: if (br.y()+br.height() > this->height() - border) { Q_ASSERT_X(diffY == 0, __func__, "diffY was already set for top border " "adjustment."); diffY = -((br.y() + br.height()) - (height() - border)); } if (diffX == 0.0 && diffY == 0.0) { pv->ensureVisible(gp, border, border); } else { // move the group: gp->moveBy(diffX, diffY); QScrollBar *sb = nullptr; if (diffX) { // Scroll all the way to the left or right // depending on whether diffX is positive or negative: sb = pv->horizontalScrollBar(); if (sb) { if (diffX > 0) { // we adjusted the left: sb->setValue(sb->minimum()); } else { // we adjusted the right: sb->setValue(sb->maximum()); } } } if (diffY) { // Scroll all the way to the top or bottom // depending on whether diffX is positive or negative: sb = pv->verticalScrollBar(); if (sb) { if (diffY > 0) { // we adjusted the top: sb->setValue(sb->minimum()); } else { // we adjusted the bottom: sb->setValue(sb->maximum()); } } } QApplication::processEvents(); } destroyItemGroup(gp); } } You will notice the extra code in DiagramScene::adjustDragPosition() which adjusts the scroll factor if needed, then calls QApplication::processEvents() to ensure that everything runs smoothly.
  • Canvas QT?

    Solved
    3
    0 Votes
    3 Posts
    375 Views
    P
    @Pl45m4 Thank you very much, all working! Appriciate your help:)
  • This topic is deleted!

    Unsolved
    2
    0 Votes
    2 Posts
    37 Views
  • Issue with Text Rendering Length Differences Using QPainter

    Solved
    7
    2 Votes
    7 Posts
    830 Views
    H
    Thanks for all the input!
  • 0 Votes
    3 Posts
    457 Views
    L
    @ChrisW67 Thanks for your response! Using Qt::FramelessWindowHint flags can help me in my case. But for further understanding, in case I want to record other apps then I have to remove its header section (such as the OBS header in above picture), I wonder If I could set this similar flags to CapturableWindow when I get it from capturableWindows(). Or QWindowCapture, QMediaRecorder had the other ways to help me with this? Please feel free to share, I’m grateful.
  • QTextEdit does not work properly when pasting rich text

    Unsolved
    1
    0 Votes
    1 Posts
    178 Views
    No one has replied
  • What is the best startegy for running one-off short tasks concurrently

    Unsolved
    6
    0 Votes
    6 Posts
    784 Views
    J
    So I found a Stack Overflow post that claims that QtConcurrent is a namespace, not a class and it is not implementable in Python, since it is template-based and templates are a C++ feature, therefore, the aforementioned Python documentation on how to use it is just auto-generated garbage and none of it actually exists in Python. Am I misunderstanding it? I suppose I am stuck to using QThread for every minor thing.... https://stackoverflow.com/questions/32378719/qtconcurrent-in-pyside-pyqt
  • How to avoid QWidget show the default background before paintEvent be called

    Unsolved
    7
    0 Votes
    7 Posts
    428 Views
    W
    @JoeCFD oh, I am not a touch screen. It is a pc with a monitor.
  • updating Chart

    Unsolved
    3
    0 Votes
    3 Posts
    285 Views
    J
    using a one shot timer works, thanks
  • Receiving serial port data in Windows not working properly without PuTTY?

    Solved
    4
    0 Votes
    4 Posts
    917 Views
    SGaistS
    @That-One-Seong I don't know the low level differences between Windows and Linux serial port handling. It could be a driver thing or simply the default behaviour of the OS.
  • Preview & Scaling in Printer Dialog

    Unsolved
    12
    0 Votes
    12 Posts
    1k Views
    SGaistS
    @ademmler no, lower level, down to the platform plugin.
  • How to check Windows domain security group membership of current user in QT?

    Unsolved
    4
    0 Votes
    4 Posts
    954 Views
    SGaistS
    @Tom-Davidson you don't change something that is in used in so many industries that would get you sued into oblivion :-) As for linking the library, I think the find_library command should help you with that.
  • QOpenglWidget memory leak

    Unsolved
    12
    0 Votes
    12 Posts
    2k Views
    JoeCFDJ
    @xingqun I did quick test with valgrind ==================5.15.2 on Ubuntu 22.04 ==556545== LEAK SUMMARY: ==556545== definitely lost: 256 bytes in 1 blocks ==556545== indirectly lost: 32 bytes in 1 blocks ==556545== possibly lost: 0 bytes in 0 blocks ==556545== still reachable: 613,875 bytes in 10,922 blocks ==556545== suppressed: 0 bytes in 0 blocks ==556545== Reachable blocks (those to which a pointer was found) are not shown. ==556545== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==556545== ==556545== For lists of detected and suppressed errors, rerun with: -s ==556545== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0) ==================6.6.2 on Ubuntu 22.04 ==557606== HEAP SUMMARY: ==557606== in use at exit: 346,588 bytes in 3,812 blocks ==557606== total heap usage: 232,769 allocs, 228,957 frees, 1,005,388,571 bytes allocated ==557606== ==557606== LEAK SUMMARY: ==557606== definitely lost: 0 bytes in 0 blocks ==557606== indirectly lost: 0 bytes in 0 blocks ==557606== possibly lost: 0 bytes in 0 blocks ==557606== still reachable: 344,572 bytes in 3,791 blocks ==557606== suppressed: 0 bytes in 0 blocks ==557606== Reachable blocks (those to which a pointer was found) are not shown. ==557606== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==557606== ==557606== For lists of detected and suppressed errors, rerun with: -s ==557606== ERROR SUMMARY: 6 errors from 4 contexts (suppressed: 0 from 0) You may try a higher version of Qt5 or switch to Qt6.
  • 0 Votes
    6 Posts
    2k Views
    SGaistS
    @Praveend hi and welcome to devnet, Which version of Qt are you trying to compile ? On which processor ?
  • Memory Management Practices Stack vs Heap for Key Objects

    Solved
    4
    0 Votes
    4 Posts
    653 Views
    W
    @Drazz Ultimately, it's not really a Qt specific question at all. More of a C++ style thing. Are you fairly new at C++ in-general? If lexical scope (i.e., between two curly braces) matches lifetime, then you normally don't want to mess around with pointers and manual allocation. It's more work for no benefit. And you introduce the possibility that you forget to free it manually. Use the heap and explicit allocation when it's necessary. For example, if you need to create something in a function and have it live past the end of that function, you can't use the stack. The QApplication exists for the scope of main(), so there's no good reason to overthink it.
  • How to customize drop-down of hovered QtComboBox in stylesheet

    Unsolved
    4
    0 Votes
    4 Posts
    437 Views
    JoeCFDJ
    @Samuel-Xue said in How to customize drop-down of hovered QtComboBox in stylesheet: QComboBox::drop-down:hover https://stackoverflow.com/questions/52782279/how-to-change-drop-down-background-color-on-qcombobox-hover
  • QRandomGenerator ULLONG_MAX

    Solved
    5
    0 Votes
    5 Posts
    417 Views
    R
    @JonB rng.bounded(unsigned int(0), ULLONG_MAX); compiles successfully. There is aproblem in qrandomh.h inline quint64 QRandomGenerator::bounded(quint64 highest) with the mask. But as Christian pointed out, the function is not suited for my usecase. Thanks
  • MS Word like behavior with QTextEdit

    Unsolved
    3
    0 Votes
    3 Posts
    255 Views
    B
    @JonB Thank you for the answer. I don't want to write my own MS Word, so it's not nessesary to serialize whole QTextDocument. I would be quite happy if I will be able to pass loaded images to QTextDocument. But it seems that it needs valid images from the disk. Sure I can create them in some temporary folder, but it does not look like a good idea.