Skip to content
  • 0 Votes
    2 Posts
    119 Views
    Christian EhrlicherC

    I would guess your QTableWidgetItem has wordWrap set to true (default). This works fine for me:

    int main(int argc, char** argv) { QApplication app(argc, argv); QTableWidget tw; tw.setRowCount(1); tw.setColumnCount(1); auto item = new QTableWidgetItem("a very long text which is elided in the middle"); tw.setItem(0, 0, item); tw.setTextElideMode(Qt::ElideMiddle); tw.setWordWrap(false); tw.show(); }
  • 0 Votes
    2 Posts
    199 Views
    C

    You should call QStyledItemDelegate::paint function inside yours QStyledItemDelegate derived class before painting of icons and text:

    void IconsRenderItemDelegate::paint(QPainter *painter,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QStyledItemDelegate::paint(painter, options, index);

    // Draw somethig
    }

  • 0 Votes
    5 Posts
    521 Views
    NarutoblazeN

    @mpergand

    Did not worked :

    setSectionResizeMode(QHeaderView::Fixed); setDefaultSectionSize(10);
  • 0 Votes
    2 Posts
    345 Views
    Christian EhrlicherC

    @CJha The grid color of a QTableView is determined by QStyle::SH_Table_GridLineColor

  • 0 Votes
    3 Posts
    547 Views
    CJhaC

    @JonB Thanks for the link to the previous question, I somehow missed it while trying to look for an answer. I will try to implement one of the methods to get the editing started signal.

  • 0 Votes
    5 Posts
    402 Views
    P

    @SGaist
    That's What i want bro Really Thank you <3

  • 0 Votes
    2 Posts
    220 Views
    Christian EhrlicherC

    According your first picture you're missing a layout on the widget.

  • 0 Votes
    4 Posts
    738 Views
    JonBJ

    @shobhitmittal said in Remove CellWidget from QTableWidget when focus out:

    As far as I understand, if I need to show a comboBox in a tableWidget cell, I need to use setCellWidget

    No, you do not need to do this (via setCellWidget()).

    Search the QTableWidget docs page, and that of QTableView from which it inherits, for edit. Qt has the "framework" for going in & out of edit mode on a cell without you having to to ever call setCellWidget. I believe that by doing that you will find that if the user clicks away from the combobox editor (which appears when editing starts) the framework will get rid of that comboxbox for you.

    Actually the editor facilities are shown under QAbstractItemView Class, from which QTreeView (and therefore QTableWidget too) inherits. You will use code including

    tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers); item->setFlags(item->flags() | Qt::ItemIsEditable);

    Those lines make an item editable by double-clicking (I think) on a cell.

    Ultimately you should use void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate). You should subclass your own QAbstractItemDelegate. Override its QWidget *QAbstractItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const. Have that return a new QComboBox for your case. Then the Qt infrastructure will be responsible for showing and destroying the combobox for you.

    Qt's Star Delegate Example provides editablity and shows what you need to do. There may be other examples. Ultimately you will be creating a QStyledItemDelegate and overriding its "editor" methods to return a combobox to use and populating that combo's items.

  • 0 Votes
    3 Posts
    627 Views
    A

    @Christian-Ehrlicher Oh, so stupid of mine for not thinking about it ahha
    thanks!

    RESULT for future noobie like me:

    for(int row=0;row<=12;row++){ QHBoxLayout *a = new QHBoxLayout(); a->addWidget((new QCheckBox(""))); QWidget *z = new QWidget(); z->setLayout(a); ui->tableWidget->setCellWidget(row,0, z); }
  • 0 Votes
    2 Posts
    470 Views
    JonBJ

    @Zhmou
    Because https://doc.qt.io/qt-6/qtablewidget.html#setItem says:

    The table takes ownership of the item.

    that means it owns it, and will dispose it if you replace it.

  • 0 Votes
    3 Posts
    375 Views
    P

    Thank You so much Bro You Are Life Save I wish All the best happy life for you <3

    note : it works better than i ever imagine <3

  • 0 Votes
    10 Posts
    1k Views
    KH-219DesignK

    Just a note about a non-Valgrind, windows-specific leak-checking method:

    https://docs.microsoft.com/en-us/visualstudio/debugger/finding-memory-leaks-using-the-crt-library?view=vs-2019

    It has been a while since I have done any extensive work on a Microsoft platform, but in my recollection crtdbg.h can do many helpful diagnostic things, although it is sometimes tricky to get all the debug build settings configured in the way that will trigger the features you seek.

  • 0 Votes
    12 Posts
    3k Views
    JonBJ

    @CEO said in How to display QTableWidget selected row in a qLineEdit of the same class?:

    @JonB now I discovered you are either hoarding knowledge or might not have done any work on the question.

    Yes, you're right. I have nothing better to do than lie to questioners about what I know and they should do, and I don't spend enough of my time writing the code demanded to save them time.

    Here is an extract from my usage of the QDataWidgetMapper which I know nothing about from a sample project of mine. I'm sorry if my variables/widgets/model are not the same as yours. Also here I happen to be binding to QSpinBoxes, let me know if I need to change it to QLineEdits for you.

    void SectorRegisters::initDataWidgetMapper() { // set up the `QDataWidgetMapper` this->dwm = new QDataWidgetMapper(this); // set model (this->sectorsModel is the model being used, in my case it's a `QStandardItemModel`) SectorsModel *sectorsModel = this->sectorsModel; dwm->setModel(sectorsModel); // Vertical => widget mapped to row dwm->setOrientation(Qt::Vertical); // current index is always column #0 dwm->setCurrentIndex(0); // Region mappings dwm->addMapping(ui->spinRegionSocialState, SectorsModel::RegionSocialState); dwm->addMapping(ui->spinRegionGoodAreas, SectorsModel::RegionGoodAreas); dwm->addMapping(ui->spinRegionPoorAreas, SectorsModel::RegionPoorAreas); dwm->addMapping(ui->spinRegionCash, SectorsModel::RegionCash); // Here there are many further widget<->model-column mappings // ... }

    You're welcome.

  • 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
    2 Posts
    840 Views
    JonBJ

    @Rhutu
    Nothing should "vanish". If you overwrite what is in QTableWidget with something not as you intended then obviously it will cease to show whatever it showed before.

    If you need to pass additional data from your main window to the slot, you can either do it via a C++ lambda for the slot, or put a slot which is in the main window instead of Obj,SLOT(WriteToFile()), and have that call Obj->WriteToFile(extra).

    Stop using old-style SIGNAL/SLOT() macros for signals/slots, use https://wiki.qt.io/New_Signal_Slot_Syntax, it's better and it will help you connect correct things.

  • 0 Votes
    9 Posts
    988 Views
    JonBJ

    @TUStudi
    Well, it's not necessarily that: you can create an item via void QTableWidget::setItem(int row, int column, QTableWidgetItem *item) instead.

  • 0 Votes
    4 Posts
    881 Views
    T

    Calling the base class implementation did the trick. Thank you again!
    I didn't try the resizeToContents as some cells shouldn't be sized to their content in some context.

  • 0 Votes
    3 Posts
    766 Views
    EagleSparrowE

    Here is the code.

    QList<SomeClass> ModeTableEntries ; for (int row = 0; row < ModeTableEntries .length(); row++) { ui->EntriesTableWidget->insertRow(row); ui->EntriesTableWidget->setRowHeight(row, 45); ModeValues value = ModeTableEntries [row]; QTableWidgetItem* currentItem = new QTableWidgetItem(value .ModeText); if(TestModeEnabled == true) currentItem->setCheckState(Qt::Checked); else currentItem->setCheckState(Qt::Unchecked); ui->EntriesTableWidget->setItem( row, 0, currentItem); currentItem->setFlags(currentItem->flags()^(Qt::ItemIsEditable )); }

    The problem is that I cannot style the default implementation of the QTableWidgetItem(QCheckbox) that is part of the QTableWidgetItem.