A bug perhaps in ui->listWidget->selectedItems().count() ?



  • It gives me an incorrect selected Items Count.
    you need to play with it a bit, 3-4 clicks will do it....press the button each time

    my below code:--
    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    ui->listWidget->addItem((char*)"1");
    ui->listWidget_2->addItem((char*)"1__");
    

    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::on_listWidget_clicked(const QModelIndex &index)
    {
    ui->listWidget_2->selectionModel()->select(index,QItemSelectionModel::Select);
    }

    void Dialog::on_listWidget_2_clicked(const QModelIndex &index)
    {
    ui->listWidget->selectionModel()->select(index,QItemSelectionModel::Select);
    }

    void Dialog::on_pushButton_clicked()
    {
    printf("%d",ui->listWidget->selectedItems().count());fflush(stdout); //output on application output screen
    }

    Starting /home/asif/Desktop/build-test__-Desktop-Debug/test__...
    1
    1
    2 //notice that it returns 2 (there is only item in the list, how did I ever select 2)
    2
    1
    2



  • Also

    void Dialog::on_pushButton_clicked()
    {
    printf("%d ",ui->listWidget->selectedItems().count());
    printf("%d\n",ui->listWidget->item(0)->isSelected());fflush(stdout);
    }

    1 1
    2 1 // it returns '2'
    2 1
    2 1
    2 1
    2 1
    2 1



  • Hi @AsifBahrainwala, welcome to devnet :)

    Interesting issue. It might be related to you using QModelIndex instances across models... I'm not sure if that's supported (QModelIndex instances can, for example, contain internal model pointers).

    I'm referring to:

    void Dialog::on_listWidget_clicked(const QModelIndex &index)
    {
    ui->listWidget_2->selectionModel()->select(index,QItemSelectionModel::Select);
    }
    
    void Dialog::on_listWidget_2_clicked(const QModelIndex &index)
    {
    ui->listWidget->selectionModel()->select(index,QItemSelectionModel::Select);
    }
    

    In on_listWidget_clicked for example, index is (presumably) a QModelIndex from the model owned by listWidget. But within that function, you're asking the listWidget_2 widget, which has a different internal model, to select the index from the first widget's model. That might not be an issue... not sure. Anyway, what I'd probably do next, is add some debug logging to see just what those items are. eg

    void Dialog::on_pushButton_clicked()
    {
        QList<QListWidgetItem *> selectedItems = ui->listWidget->selectedItems();
        printf("%d",selectedItems.count());fflush(stdout); //output on application output screen
        if (selectedItems.count() > 1) {
            foreach (QListWidgetItem * item, selectedItems) {
                printf("%p %s",item, item->text().toLocal8Bit().data()); fflush(stdout);
            }
        }
    }
    

    Cheers.



  • @Paul-Colby Thanks
    probably I should have used

    I am relatively new to QT , been more of an GTK/glade guy ....but this is really good stuff....

    void Dialog::on_listWidget_clicked(const QModelIndex &index)
    {
    ui->listWidget_2->item(index.row())->setSelected(true);
    }

    void Dialog::on_listWidget_2_clicked(const QModelIndex &index)
    {
    ui->listWidget->item(index.row())->setSelected(true);
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.