Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A bug perhaps in ui->listWidget->selectedItems().count() ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AsifBahrainwalaA Offline
    AsifBahrainwalaA Offline
    AsifBahrainwala
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • AsifBahrainwalaA Offline
      AsifBahrainwalaA Offline
      AsifBahrainwala
      wrote on last edited by
      #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

      1 Reply Last reply
      0
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        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.

        AsifBahrainwalaA 1 Reply Last reply
        0
        • Paul ColbyP Paul Colby

          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.

          AsifBahrainwalaA Offline
          AsifBahrainwalaA Offline
          AsifBahrainwala
          wrote on last edited by
          #4

          @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);
          }

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved