Skip to content
  • 0 Votes
    6 Posts
    938 Views
    Pl45m4P

    @CJha

    https://stackoverflow.com/questions/26614678/validating-user-input-in-a-qtableview
    (will work for every model/view, I think)

    Here you go :)

  • 0 Votes
    8 Posts
    2k Views
    A

    @Christian-Ehrlicher True and this is what I did. It works:

    void MainWindow::removeItem(const QString &text) { for (int i = 0; i < ui->listWidget->count(); ++i) { auto item = ui->listWidget->item(i); auto itemWidget = dynamic_cast<CustomWidget*>(ui->listWidget->itemWidget(item)); if (itemWidget->getText() == text){ delete item; break; } } }
  • 0 Votes
    4 Posts
    2k Views
    B

    @learnist said in How to edit a QListwidgetItem by using custom context menu ? need some corrections:

    but for point 3 ,which edit trigger should i use ??
    can you please elaborate

    If you don't want double clicking or any key pressing to trigger the editing, just set it to QAbstractItemView::NoEditTriggers.

  • 0 Votes
    5 Posts
    536 Views
    SGaistS

    Good then please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found.

  • 0 Votes
    3 Posts
    739 Views
    CybeXC

    @VRonin

    Doing a few (rather simple) tests, I found the following (which confirms your suspicion)

    Works: (but no check box) - this is expected to work

    // create item QListWidgetItem* item = new QListWidgetItem(name, ui->listDriveInfo); // item->setText(name); // item->setFlags(item->flags() | Qt::ItemIsUserCheckable); // item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked); // add item to list ui->listDriveInfo->addItem(item);

    this crashes on setFlags() call

    // create item QListWidgetItem* item = new QListWidgetItem(name, ui->listDriveInfo); // item->setText(name); //set checkable and state Qt::ItemFlags localFlags = item->flags(); item->setFlags(localFlags | Qt::ItemIsUserCheckable); item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked); // add item to list ui->listDriveInfo->addItem(item);

    but these 2 both work (without setting owning QListWidget)

    // create item QListWidgetItem* item = new QListWidgetItem(name); // item->setText(name); //set checkable and state Qt::ItemFlags localFlags = item->flags(); item->setFlags(localFlags | Qt::ItemIsUserCheckable); item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked); // add item to list ui->listDriveInfo->addItem(item);

    and

    // create item QListWidgetItem* item = new QListWidgetItem(); // item->setText(name); //set checkable and state Qt::ItemFlags localFlags = item->flags(); item->setFlags(localFlags | Qt::ItemIsUserCheckable); item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked); // add item to list ui->listDriveInfo->addItem(item);

    Testing:

    I create a new application, did exactly the same (add a string and a checkbox) and that work 100%. Create a form, throw on a
    QListWidget and run it with this code.

    QListWidgetItem* item = new QListWidgetItem("New", ui->listWidget); item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setCheckState(Qt::Unchecked); QListWidgetItem* item1 = new QListWidgetItem("New 1", ui->listWidget); item1->setFlags(item->flags() | Qt::ItemIsUserCheckable); item1->setCheckState(Qt::Unchecked); QListWidgetItem* item2 = new QListWidgetItem("New 2", ui->listWidget); item2->setFlags(item->flags() | Qt::ItemIsUserCheckable); item2->setCheckState(Qt::Unchecked); ui->listWidget->addItem(item); ui->listWidget->addItem(item1); ui->listWidget->addItem(item2); connect(ui->listWidget, &QListWidget::itemClicked, this, [this](QListWidgetItem * i) { // Get drive info from driveList int pos = ui->listWidget->row(i); // check state & set state bool active = i->checkState() == Qt::Checked; qDebug() << (active ? "enabled" : "disabled"); });

    So my question is, bug (on whose side) or some setting on my side? Thoughts on what to look for?

  • 0 Votes
    6 Posts
    2k Views
    R_IrudezuR

    @Ratzz Sorry i can not check your answer as solved answer, Qt forum has a bug about that. I solved my problem, i've already read the topic that you linked here but after you posted it then a read again and i solved my problem thank you, but even so trying to give more explanatory answers is a good way because i like this forum and i want to see answers from another angles, another perspectives. When i get experienced at Qt i want to give good answers too. :)

    And thank you so much Mr. @dheerendra . I think approach with row is a better way but i was need to a quick way so that stackoverflow link helped me about that.

  • 0 Votes
    8 Posts
    3k Views
    raven-worxR

    @philm001
    the stylesheet declaration should rather look like this i guess:

    w->setStyleSheet(QString("QListView::item { background: rgba(255, 0, 0, %1)}").arg(value.toInt())
  • 0 Votes
    5 Posts
    4k Views
    T

    Well seems that it needs to emit model's layoutChanged() to do what I need. I created new class which inherits QListWidget and there I connected signal to layoutChanged signal of model() object and it worked.

  • 1 Votes
    7 Posts
    32k Views
    VRoninV

    @kishore_hemmady said in QListWidget delete selected item:

    how can i achieve this by selecting the check Box rather than selecting the list content?

    for(int i=ui->listWidget->model()->rowCount()-1;i>=0;--i){ if(ui->listWidget->model()->index(i,0).data(Qt::CheckStateRole).toInt()==Qt::Checked) ui->listWidgetMy->model()->removeRow(i); }
  • 0 Votes
    7 Posts
    3k Views
    mrjjM

    @VRonin
    Oh and you provide a model based solution. Much more elegant :)
    https://forum.qt.io/topic/72974/link-two-qlistwidget/4

  • 0 Votes
    12 Posts
    5k Views
    SGaistS

    It's good when it's more friendly :)

  • 0 Votes
    2 Posts
    840 Views
    kshegunovK

    @WhatIf

    Can someone show me an example of what other pieces of information it can hold? What type?

    Any type that's registered for QVariant (declared as a meta-type). Here's what Qt has a data roles, but you can have your own.

    Kind regards.

  • 0 Votes
    2 Posts
    2k Views
    M

    I fixed this issue depending on the mentioned snippet here:
    http://www.qtcentre.org/threads/53580-TapAndHoldGesture-sender-object-name

  • 0 Votes
    2 Posts
    1k Views
    SGaistS

    Hi,

    Why not use the parameters of currentItemChanged to get the current item ?

  • 0 Votes
    4 Posts
    2k Views
    SGaistS

    Then you should also implement the sizeHint method of your delegate

  • 0 Votes
    7 Posts
    7k Views
    SGaistS

    For that kind of things the combo:
    QListView
    QStringListModel
    QIdentityProxyModel << Customize the icons here

    would do it in a simpler way.

    As for items being QObject derived classes, rather not. The various QXXXItem classes must be as lightweight as possible since there can be several thousands of them and QObject has a cost and constraints. Except maybe in a few corner cases there would be no real benefit to have it there for the common usage.