Skip to content
  • 0 Votes
    3 Posts
    141 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
    2 Posts
    159 Views
    JonBJ

    @johnco3
    I'm afraid I know nothing about std::variant, but a couple of observations.

    Your setData() is void. QStandardItemModel's is bool. Have you checked the return result when you call that, you never know if it might be barfing?

    Your setData() defaults to Qt::UserRole + 1. How does your item->data().value<PortVariant>() pass that role to fetch?

    setData() copies its argument. I assume the equivalent code to test would be something like:

    PortVariant portVariant = port; QVariant v = QVariant::fromStdVariant(portVariant); QVariant v2 = v1; const auto& portVariant = v2.value<PortVariant>();

    How does that come out? You might also call QVariant::canConvert<>() to check, including on your item->data().

    I assume all the various component types in your structs are serializable to QVariant without further registration.

  • 0 Votes
    13 Posts
    757 Views
    D

    @JonB I'm in python, I cant just do stream >> QVector<QStandardItemData> I'm afraid :/
    I'd have to deserialize each data object by hand to find correct offset byte to next role in vector I think

    Update!
    Welp turns out there is readQVariant!

    i = QStandardItem("hello") i.setData("dasdas", 15241) i.setData(5325, 15242) i.setData(["vdsvds"], 15243) i.setData(["vdsvds"], 15245) ar = QByteArray() s = QDataStream(ar, QIODevice.ReadWrite) i.write(s) r = QDataStream(ar, QIODevice.ReadOnly) val = 0 v = r.readUInt32() for a in range(v): role = r.readInt32() var = r.readQVariant() print(role, var) print(v)

    This works, wop wop!

  • 0 Votes
    2 Posts
    115 Views
    SGaistS

    Hi,

    I would venture to say no :-)

    Which version of Qt are you using ?
    In which OS ?
    Are doing the move at the same level ?
    Do you have a filter ?

  • 0 Votes
    7 Posts
    449 Views
    BeaverShallBurnB

    @JonB

    Huge thanks, you solved the puzzle for me!

  • 0 Votes
    3 Posts
    357 Views
    S

    @VRonin Thanks.
    It worked.
    We have used delegates because we have combobox, lineedit also.
    So we have put check for combobox, not to update via delegate.

  • 0 Votes
    4 Posts
    451 Views
    Christian EhrlicherC

    @Joel-Bodenmann said in QStandardItem custom drag MIME:

    However, I'd like to understand why QStandardItem does not offer a setMimeData() function.

    Because noone wants to create a mime data just on suspicion for every item.

  • 0 Votes
    2 Posts
    2k Views
    Chris KawaC

    which style parameter do I have to edit to control the color of item when the widget loses focus?

    QTreeView::item:active { color: red; } /* When widget has focus*/ QTreeView::item:!active { color: blue; } /* When widget doesn't have focus*/

    if I do show-decoration-selected: 1; How do I control decoration color ?

    It depends on the platform styling. For example on Windows it only colors on hover the little arrow that expands/collapses the branch and that arrow is painted by a system style call so you can't control its color. You can however replace the system styling and change the branch images to your own with any color you like. See Customizing QTreeView for examples.

  • 0 Votes
    9 Posts
    5k Views
    S

    @JonB
    I added comments in my code. I think I should explain it clearly. Sorry... but please help.

  • 0 Votes
    3 Posts
    3k Views
    S

    @SGaist said in Add Qlabel as item in QTableview:

    QStyledItemDelegate

    Ok..I'll try with that.
    from google search maybe this kind of output it will give, which maybe as our requirement.
    0_1558578799369_da2ccc76-385d-40ee-954a-79cbbe227dc3-image.png

    Else, I have some lengthy approach..implement custom label with mousepressevent handled which emits the object name and add these custom labels, 20 in HLayout and add hlayouts in a vlayout...

    Thanks!

  • 0 Votes
    15 Posts
    3k Views
    JonBJ

    @n-2204
    Please try to format your posts readably. For lines of code use the Code tag when posting, or but lines of 3-backticks above & below.

    You are also now asking this same question in a new thread you have created (https://forum.qt.io/topic/125774/qtableview-how-to-make-noneditable-column). You should stick to one thread, not create multiple ones.

    where i should give like it will be applicable for table1 or table2

    I don't know what you mean. To use the flags() approach you must sub-class QStandardItemModel, which I assume is what your GAS_tool is, else this will have no effect.

    If you want to use the setFlags() approach, you do not need to sub-class, but must set the flags explicitly on each item which is to be non-editable.

    I also wrote this in your other thread.

  • 0 Votes
    4 Posts
    643 Views
    D

    @dheerendra thanks! I had a feeling that was the case, but QSortFilterProxyModel is a very interesting discovery! I used it a few times but humh... I will look into that more! Thanks! If you have any suggestions/tutorials/read please shoot away, bit lost atm as to what to look for there.

    @Eeli-K Yep, I subclass QAbstractItemModel & created my own treeItem from the ground up so I have full control over these 2 entities. I Extended QtreeView with some "helper" handlers of my data/model but that's it. Did not touch QModelIndex to me its black magic ;- ) Will think about solving that internally somehow. If I were to redirect the read of some widgets, what would I need to extend more? index/data/row functions? Hmm I need to think about it more. Getting some lightbulbs slowly here but I wonder if it's backward. Atm, I'm thinking that each of the virtual functions I had to overload would have to return native model data 1st and once that's done, return the instanced model/items data... uhhh getting complicated humhhhh humhhh humhhhh interesting. Thanks!

    Regards
    Dariusz
    TIA

  • 0 Votes
    27 Posts
    5k Views
    JKSHJ

    @elfring said in Returning C++ references from more programming interfaces?:

    For example, why should Qt provide extensions that break encapsulation and increase the risk of errors?

    I suggest to use algorithms which can work together with container classes at more source code places.

    You did not address any of the concerns. You only added suggestions.

    That is not acceptable. You must only submit ideas/proposals that don't break encapsulation.

  • 0 Votes
    8 Posts
    1k Views
    E

    How do you think about the usage of customised constructor implementations for the class “QStandardItem”?

  • 0 Votes
    27 Posts
    5k Views
    VRoninV

    @Christian-Ehrlicher said in Support for QStandardItem proxies?:

    But only when Qt was compiled in debug mode

    Yes, as the docs say using invalid indexes remains undefined behaviour, I am just trying to help users spot these cases

  • 0 Votes
    5 Posts
    1k Views
    E

    Unless you go into very specific obscure cases

    Another software design possibility would be to fiddle with proxies for data models (and their items), wouldn't it?

  • 0 Votes
    27 Posts
    5k Views
    JKSHJ

    @elfring said in Support for constructing QStandardItem objects from QVariant references?:

    I would appreciate if I can reuse existing functionality from a higher level base class.

    Sorry! I just re-read this line and realized you said "higher level base class". In this case, please ignore what I said about choosing pros and cons.

  • 0 Votes
    4 Posts
    2k Views
    VRoninV

    What model are you using? QStandardItemModel had the roles argument working from Qt 5.11 onward. An empty role vector implies "all roles changed"

  • 0 Votes
    5 Posts
    2k Views
    Y

    Thank you @mrjj and @Christian for answer. I am able to make user Checkable.

    class CustomListModel : public QStandardItemModel {
    public:
    Qt::ItemFlags CustomListModel::flags(const QModelIndex & index) const {
    Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index);
    if (index.isValid()) {
    return defaultFlags | Qt::ItemIsUserCheckable;
    }
    return defaultFlags;
    }

    CustomListModel(const int row, const int coloumn) : QStandardItemModel(row, coloumn) { }

    };

    and modification.
    hwListProxyModel->setSourceModel(new CustomListModel (0, 0));

  • 0 Votes
    5 Posts
    2k Views
    CybeXC

    @SGaist
    Platform - Linux x86_64
    Qt Version - 5.9.3

    @JNBarchan
    fontHeight produced a integer value +/- 56
    iconHeight produced an integer value ( < fontHeight)

    @VRonin
    The Role's where not the problem. Please see my updated delegate below (kudos for calling it, the delegate being the problem)

    Well this is embarrasing. I did not fully realize what I was coding until I had battled with the delegate all night.

    I understood that I was coding a template, and assumed that on each item which was added, the origin remained at [0,0]. This was not the case as there was an offset added of value specified in the sizeHint (in my case, 56). Thus all my QListView's QStandardItem's were in fact there, but drawn over each other.

    After changing values and experimenting over a long period (a number of hours), I finally came to the desired result, shown below.

    Also, I need to thank scopchanov for his hint into this offset problem

    StackoverFlow question as a reference

    Updated ServerDelegate.cpp

    #include "serverdelegate.h" ServerDelegate::ServerDelegate(QStyledItemDelegate *parent) : QStyledItemDelegate(parent) { fontCountry = QApplication::font(); fontCountry.setBold(true); fontCountry.setPointSize(QApplication::font().pointSize() + 3); fontCity = QApplication::font(); fontCity.setItalic(true); fontCity.setPointSize(QApplication::font().pointSize() - 1); } ServerDelegate::~ServerDelegate(){ } QSize ServerDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{ Q_UNUSED(index) QSize totalCountrySize = Global::getCountryFlagFromCache(index.data(DataRole::CountryText).toString()).size(); QSize totalSideIcon = QPixmap(":/res/images/premium", "PNG").size(); QFontMetrics fmCountry(fontCountry); QFontMetrics fmCity(fontCity); int fontHeight = (2 * AppGlobal::Style_List_Seperator_Width) + (2 * AppGlobal::Style_List_Text_Item_Margin) + fmCountry.height() + fmCity.height(); int iconHeight = (2 * AppGlobal::Style_List_Seperator_Width) + (totalCountrySize.height() > totalSideIcon.height() ? totalCountrySize.height() : totalSideIcon.height()); int height = (fontHeight > iconHeight) ? fontHeight : iconHeight; int width = option.rect.width(); QSize size = QSize(width, height); return size; } void ServerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QStyledItemDelegate::paint(painter, option, index); QFontMetrics fmCountry(fontCountry); QFontMetrics fmCity(fontCity); QRect rec = option.rect; painter->save(); painter->setClipRect(rec); QString countryText = index.data(DataRole::CountryText).toString(); QString cityText = index.data(DataRole::CityText).toString(); QPixmap countryFlag = QPixmap(qvariant_cast<QPixmap>(index.data(DataRole::CountryFlag))); QPixmap sideIcon = qvariant_cast<QPixmap>(index.data(DataRole::SideIconFlag)); // Get a rectangle by size x, y. // With cooridinates [0,0]; [x,0]; [x,y]; [0,y] QRect topLine = option.rect, bottomLine = option.rect; // create top 'seperator' of X px's width, green in color; topLine.setTop(rec.top()); topLine.setLeft(rec.left()); topLine.setRight(rec.right()); topLine.setBottom(rec.top() + AppGlobal::Style_List_Seperator_Width); // 1px down painter->setPen(AppGlobal::Style_List_Seperator_Color); painter->fillRect(topLine, AppGlobal::Style_List_Seperator_Color); painter->drawRect(topLine); // create bottom 'seperator' of X px's width, green in color; bottomLine.setTop(rec.bottom() - AppGlobal::Style_List_Seperator_Width); bottomLine.setLeft(rec.left()); bottomLine.setRight(rec.right()); bottomLine.setBottom(rec.bottom()); // 1px down painter->setPen(AppGlobal::Style_List_Seperator_Color); painter->fillRect(bottomLine, AppGlobal::Style_List_Seperator_Color); painter->drawRect(bottomLine); // create background rectangle QRect content(rec.left(), topLine.bottom(), (rec.right() - rec.left()), (bottomLine.top() - topLine.bottom())); painter->setPen(AppGlobal::Style_List_Background_Color); painter->fillRect(content, ((option.state & QStyle::State_MouseOver) ? AppGlobal::Style_List_Hover_Color : AppGlobal::Style_List_Background_Color )); painter->drawRect(content); // create content rectangles from content container. QRect rectCountryFlag = content, rectSideIcon = content; // create country icon rectangle QSize countryFlagSize = countryFlag.size(); int cFPos = ((rectCountryFlag.bottom() - rectCountryFlag.top()) / 2) - (countryFlagSize.height() / 2) - 8; rectCountryFlag.setTop(rectCountryFlag.top() + cFPos); rectCountryFlag.setBottom(content.bottom() - cFPos); rectCountryFlag.setLeft(AppGlobal::Style_List_Left_Item_Margin - 8); rectCountryFlag.setRight(AppGlobal::Style_List_Left_Item_Margin + 16 + countryFlagSize.width()); painter->drawPixmap(rectCountryFlag, countryFlag); // create side icon rectangle QSize sideIconSize = sideIcon.size(); int siPos = ((rectSideIcon.bottom() - rectSideIcon.top()) / 2) - (sideIconSize.height() / 2) - 4; rectSideIcon.setTop(rectSideIcon.top() + siPos); rectSideIcon.setBottom(content.bottom() - siPos); rectSideIcon.setLeft(rec.width() - (AppGlobal::Style_List_Right_Item_Margin + 8 + sideIconSize.width())); rectSideIcon.setRight(rec.width() - AppGlobal::Style_List_Right_Item_Margin); painter->drawPixmap(rectSideIcon, sideIcon); int textContentLeft = rectCountryFlag.right() + AppGlobal::Style_List_Text_Item_Margin + AppGlobal::Style_List_Left_Item_Margin, textContentTop = content.top() + AppGlobal::Style_List_Text_Item_Margin; const QRect textContent( textContentLeft , textContentTop, (rectSideIcon.left() - AppGlobal::Style_List_Text_Item_Margin) - textContentLeft, (content.bottom() - AppGlobal::Style_List_Text_Item_Margin) - textContentTop); // create country text rectangle QRect rectCountryText = content, rectCityText = content; rectCountryText.setLeft(textContent.left()); rectCountryText.setTop(textContent.top()); rectCountryText.setRight(textContent.right()); rectCountryText.setBottom(textContent.top() + fmCountry.height()); painter->setPen(AppGlobal::Style_Heading_Color); painter->setFont(fontCountry); painter->drawText(rectCountryText, countryText); // create city text rectangle rectCityText.setLeft(textContent.left() + ( 2 * AppGlobal::Style_List_Text_Item_Margin)); rectCityText.setTop(rectCountryText.bottom()); rectCityText.setRight(textContent.right()); rectCityText.setBottom(textContent.bottom() + fmCity.height()); painter->setPen(AppGlobal::Style_SubText_Color); painter->setFont(fontCity); painter->drawText(rectCityText, cityText); // restore painter painter->restore(); }