Skip to content
  • 0 Votes
    3 Posts
    101 Views
    J

    Ah yes, of course, thats makes a lot of sense.
    I'll set the item as selected, and change the way i draw a selected item to match that of a hovered item.

    Thank you i think i was trying to overcomplicate things.

  • 0 Votes
    8 Posts
    467 Views
    S

    @JonB @SGaist @DerReisende Thank you all very much. Thats kinda tedious to do but guess thats how it is. Appreciate all your help and indeed mapFrom and mapToSource are what fixes my issue. :D Y'all have a nice day.

  • 0 Votes
    8 Posts
    416 Views
    SGaistS

    @Jammin44fm the paint method of QGraphicsItem is public so you can call it yourself with the painter you use in the delegate.

  • 0 Votes
    26 Posts
    3k Views
    SGaistS

    @StudentScripter great !

    Then please mark the model as solved using the "Topic Tools" button or the dotted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)

  • 0 Votes
    3 Posts
    237 Views
    S

    @Pl45m4 Worked like a charm, thank you very much. :)

  • 0 Votes
    1 Posts
    134 Views
    No one has replied
  • 0 Votes
    19 Posts
    1k Views
    S

    @Chris-Kawa thank you very much. That definitely helped a lot. :)

    I have done the manual positioning now, so all delegates line up with the actual widget. :)

    void ViewLayerItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); // Überprüfen Sie, ob der aktuelle Index bearbeitet wird if (index == currentlyEditedIndex) { return; } // Setzen Sie die Werte der SpinBox und CheckBox basierend auf den Modellwerten QString lineEditvalue = index.model()->data(index, Qt::EditRole).toString(); bool checkBoxValue = index.model()->data(index, Qt::CheckStateRole).toBool(); // Laden Sie das Icon und skalieren Sie es QPixmap iconPixmap("://resource/quick.png"); // Ersetzen Sie dies durch den Pfad zu Ihrer Icon-Datei QPixmap scaledPixmap = iconPixmap.scaled(32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Berechnen Sie die Position für das Icon int centerY = option.rect.top() + option.rect.height() / 2; int iconY = centerY - scaledPixmap.height() / 2; QPoint iconPos = QPoint(option.rect.left() + 10, iconY); // Zeichnen Sie das Pixmap mit dem QPainter painter->drawPixmap(iconPos, scaledPixmap); // Berechnen Sie die Position und Größe für das LineEdit QRect lineEditRect = option.rect; lineEditRect.setLeft(iconPos.x() + scaledPixmap.width() + 10); // Adjust as needed lineEditRect.setRight(option.rect.right() - 10); // Adjust as needed // Erstellen Sie ein QStyleOptionFrame für das LineEdit QStyleOptionFrame lineEditOption; lineEditOption.lineWidth = 1; // Setzen Sie die Liniendicke auf 1 lineEditOption.rect = lineEditRect; // Zeichnen Sie das LineEdit QApplication::style()->drawControl(QStyle::CE_ShapedFrame, &lineEditOption, painter); // Zeichnen Sie den Text des LineEdits painter->drawText(lineEditOption.rect.adjusted(2,0,0,0), Qt::AlignLeft | Qt::AlignVCenter, lineEditvalue); // Berechnen Sie die Position und Größe für die CheckBox QRect checkBoxRect = option.rect; checkBoxRect.setLeft(lineEditRect.right() - 22); // Adjust as needed checkBoxRect.setRight(option.rect.right() - 10); // Adjust as needed // Erstellen Sie ein QStyleOptionButton für die CheckBox QStyleOptionButton checkBoxOption; checkBoxOption.state = checkBoxValue ? QStyle::State_On : QStyle::State_Off; checkBoxOption.rect = checkBoxRect; // Zeichnen Sie die CheckBox QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter); }
  • 0 Votes
    5 Posts
    394 Views
    SGaistS

    @StudentScripter For the grouping you have to use an adequate data structure.

    As for hovering, IIRC, you have to have mouse tracking enabled on the view.

  • 0 Votes
    3 Posts
    229 Views
    Y

    Remove space; Inside the table cell but outside my custom widget

    Here some code snap. @ChrisW67

    #include "ElementsListWidget.h" #include "AbstractElement.h" #include "ElementDragEventHandler.h" #include <QAbstractScrollArea> #include <QGridLayout> #include <QLabel> #include <QPixmap> #include <QScrollArea> #include <QSortFilterProxyModel> #include <QVariant> #include <qscrollbar.h> #include <qstandarditemmodel.h> #include <qtreeview.h> ElementsListWidget::ElementsListWidget(QWidget* parent) : QWidget(parent) { QVBoxLayout* mainlayout = new QVBoxLayout(this); mainlayout->setContentsMargins(0, 0, 0, 0); elementsListLayout = new QTreeView(this); elementsListLayout->setContentsMargins(0, 0, 0, 0); elementsListLayout->setHeaderHidden(true); elementsListLayout->setIndentation(0); elementsListLayout->setUniformRowHeights(false); elementsListLayout->setObjectName("ElementsListWidget"); elementsListLayout->setSelectionMode(QAbstractItemView::NoSelection); elementsListLayout->verticalScrollBar()->parent()->setProperty("background_transparent", true); elementsListLayout->horizontalScrollBar()->parent()->setProperty("background_transparent", true); sourceModel = new QStandardItemModel(this); elementsListLayout->setModel(sourceModel); mainlayout->addWidget(elementsListLayout); setLayout(mainlayout); elementsPerRow = 3; totalModelElements = 0; sourceModel->setColumnCount(elementsPerRow); } void ElementsListWidget::addElement(AbstractElement* element) { elementsModel << element; showWidgetOnView(createDisplayUnit(element)); } QWidget* ElementsListWidget::createDisplayUnit(AbstractElement* element) { if (!element) { return nullptr; } QWidget* elementDispalyWidget = new QWidget; elementDispalyWidget->setObjectName("elementDisplayUnit"); QVBoxLayout* elementdisplayLay = new QVBoxLayout(elementDispalyWidget); //elementdisplayLay->setContentsMargins(0, 0, 0, 0); QLabel* elementIconLabel = new QLabel(elementDispalyWidget); elementIconLabel->setObjectName("elementIconLabel"); elementIconLabel->setProperty("element", QVariant::fromValue(static_cast<AbstractElement*>(element))); elementIconLabel->setCursor(Qt::OpenHandCursor); elementIconLabel->setAlignment(Qt::AlignCenter); elementIconLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); elementIconLabel->installEventFilter(new ElementDragEventHandler(elementIconLabel)); // Install event filter elementIconLabel->setPixmap(element->getImage().scaled(50, 50)); QLabel* elementNameLabel = new QLabel(element->getName(), elementDispalyWidget); elementNameLabel->setObjectName("elementNameLabel"); elementNameLabel->setAlignment(Qt::AlignCenter); elementNameLabel->setWordWrap(true); elementdisplayLay->setAlignment(Qt::AlignHCenter); elementdisplayLay->addWidget(elementIconLabel); elementdisplayLay->addWidget(elementNameLabel); elementdisplayLay->addStretch(); elementDispalyWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); return elementDispalyWidget; } void ElementsListWidget::setElementsPerRow(int numberOfelementsPerRow) { if (numberOfelementsPerRow > 0) { elementsPerRow = numberOfelementsPerRow; } } void ElementsListWidget::serachElement(const QString& searchText) { clearModel(); // Iterate through the stored widgets in elementWidgetModel for (AbstractElement* element : elementsModel) { if (element && element->getName().contains(searchText, Qt::CaseInsensitive)) { showWidgetOnView(createDisplayUnit(element)); } } } void ElementsListWidget::showWidgetOnView(QWidget* elementDisplayUnit) { if (elementDisplayUnit == nullptr) { return; } QStandardItem* item = new QStandardItem; // Calculate the row and column count for the new element int row = totalModelElements / elementsPerRow; int column = totalModelElements % elementsPerRow; sourceModel->setItem(row, column, item); elementsListLayout->setIndexWidget(item->index(), elementDisplayUnit); totalModelElements++; } void ElementsListWidget::clearModel() { sourceModel->clear(); totalModelElements = 0; }

    styel sheet for few widget

    QWidget#ElementsListWidget { background-color: #DAE0E9; color: #3B5571; border: none; } #elementDisplayUnit { background-color: transparent; font-size: 14px; } #elementIconLabel { width: 64px; background-color: #ffffff; border: 1px solid #BEC9CD; height: 64px; position: relative; /* align-items: center; */ border-radius: 5px; /* justify-content: center; */ background-color: #FFFFFF; } #elementIconLabel:hover { background-color: lightgray; } #elementNameLabel { padding: 0px 0 0 0; font-size: 11px; max-width: 64px; text-align: center; line-height: 12px; /* text-overflow: ellipsis; */ background-color: transparent; /* word-wrap: break-word; */ font-weight: bold; }
  • 0 Votes
    1 Posts
    214 Views
    No one has replied
  • 0 Votes
    5 Posts
    307 Views
    Fire CubeF

    Many Thanks
    That worked great.

  • 0 Votes
    7 Posts
    636 Views
    BeaverShallBurnB

    @JonB

    Huge thanks, you solved the puzzle for me!

  • 0 Votes
    3 Posts
    1k Views
    Joel BodenmannJ

    @Gojir4 That worked well. I'm not sure how I could have missed this simple solution... Thank you a lot!

  • 0 Votes
    3 Posts
    291 Views
    JonBJ

    @Dariusz
    Interesting :) I guess it generates it only on demand, and doesn't do so during model reset.

  • 0 Votes
    3 Posts
    526 Views
    D

    @VRonin I want to do some heavy changes/customisations... stuff I cant do in CSS...
    Like moving expand button to opposite of tree, changing location of checkbox, changing indentation of child items.

    How can I control indentation of child items? I need it now :/ I keep getting "invisible" hitboxes as my child item is moved to left visually but hitboxes are drawn in original places :///

    Ideas?

    I just realized there is "setIndentation"... sigh............ Ok that kinda works, how can I set indentation per row? :D

  • 0 Votes
    2 Posts
    317 Views
    F

    A colleague of mine, that used Qt for years, helped me.
    The culprit was (other than me) that I wrongly created the parent of the children.

  • 0 Votes
    1 Posts
    824 Views
    No one has replied
  • 0 Votes
    3 Posts
    453 Views
    J

    I've managed to hack together my own solution,
    (based on looking at the source )

    Using my own QBasicTimer, and overriding the
    void timerEvent(QTimerEvent *event) and
    void dragMoveEvent(QDragMoveEvent *event)

    it's a bit ugly but it works.
    it would be nice if the default mechanism offered the option to make the time expand only.

    Cheers,

  • 0 Votes
    4 Posts
    987 Views
    VRoninV

    @ralphtink said in How to flatten a TreeView to show only the leaf node items?:

    I don't want to import a whole library just for this

    I would strongly suggest you do but even if you want to go against my advice you can just:

    take kbihash_p.h, kdescendantsproxymodel.h and kdescendantsproxymodel.cpp add the files to your project replace #include "kitemmodels_export.h" with #define KITEMMODELS_EXPORT satisfy LGPL 2 requirement on the imported code
  • 0 Votes
    12 Posts
    1k Views
    J

    Okay, I had absolutely believed that I had tried making the call to my savePos() function to try to diagnose whether the signal had something to do with it, yet when I disable the connect()ion altogether and call it explicitly, now these APIs want to behave like good little children and do what they're supposed to.

    So, essentially I shot myself in the foot by handling this with modelAboutToBeReset(), which implicitly trashes the internal cache of visible indexes.

    D'oh! Argh, etc.

    Thanks for the help, folks.