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. How to change QTableView data based on selection in QComboBox.

How to change QTableView data based on selection in QComboBox.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtableview c++qcomboboxqitemselectionselection modelmodel-view
4 Posts 3 Posters 1.2k Views
  • 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.
  • J Offline
    J Offline
    jaivardhanf
    wrote on 11 Dec 2022, 03:23 last edited by
    #1

    I have a QTableView which gets its data from QAbstractTableModel and is a 2d array. Below the TableView I have two comboboxes. User first selects the row in the TableView. Now I want that when the user selects an option from the combobox, I want the selected option to update the display in the TableView selection.

    Shown below is the UI. For example if the User Selects REF DES in the class combo box I want the selected row to update its class name to REF DES.

    How do I achieve this?

    test.PNG .

    J 1 Reply Last reply 11 Dec 2022, 08:03
    0
    • J jaivardhanf
      11 Dec 2022, 03:23

      I have a QTableView which gets its data from QAbstractTableModel and is a 2d array. Below the TableView I have two comboboxes. User first selects the row in the TableView. Now I want that when the user selects an option from the combobox, I want the selected option to update the display in the TableView selection.

      Shown below is the UI. For example if the User Selects REF DES in the class combo box I want the selected row to update its class name to REF DES.

      How do I achieve this?

      test.PNG .

      J Offline
      J Offline
      JonB
      wrote on 11 Dec 2022, 08:03 last edited by JonB 12 Nov 2022, 10:31
      #2

      @jaivardhanf
      Hello and welcome.

      Attach a slot to the void QComboBox::currentTextChanged(const QString &text) signal. In the slot find which row is selected in the model and setData() the class column to the value from the combobox.

      1 Reply Last reply
      2
      • J Offline
        J Offline
        jaivardhanf
        wrote on 12 Dec 2022, 02:15 last edited by
        #3

        So below is my code. I don't know why it is not updating. I implemented a slot called textchanged which gets emitted when currenttextchanged in my combo box.

        void GetOptionsClass::TextChanged(const QString& text) {
        int rowidx = ui->line_tableView->selectionModel()->currentIndex().row();
        QModelIndex nIndex = model->index(rowidx, 2);
        model->setData(nIndex, text, Qt::EditRole);
        }

        Below is my setData and flags methods.

        bool LineModel::setData(const QModelIndex& index, const QVariant& value, int role) {
        if (role == Qt::EditRole && index.column() == 2 && index.isValid())
        {
        Line_map[index.row()][index.column()] = value.toString();
        QModelIndex top = createIndex(index.row(), 0);
        QModelIndex bottom = createIndex(index.row(), 3);

        	emit dataChanged(top, bottom);
        	return true;
        }
        return false;
        

        }

        Qt::ItemFlags LineModel::flags(const QModelIndex& index) const
        {
        return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
        }

        This is how I connect the signal and slot

        connect(ui->lineClass, &QComboBox::currentTextChanged, this, &GetOptionsClass::TextChanged);

        Can you please point out what I am doing wrong

        J 1 Reply Last reply 12 Dec 2022, 02:38
        0
        • J jaivardhanf
          12 Dec 2022, 02:15

          So below is my code. I don't know why it is not updating. I implemented a slot called textchanged which gets emitted when currenttextchanged in my combo box.

          void GetOptionsClass::TextChanged(const QString& text) {
          int rowidx = ui->line_tableView->selectionModel()->currentIndex().row();
          QModelIndex nIndex = model->index(rowidx, 2);
          model->setData(nIndex, text, Qt::EditRole);
          }

          Below is my setData and flags methods.

          bool LineModel::setData(const QModelIndex& index, const QVariant& value, int role) {
          if (role == Qt::EditRole && index.column() == 2 && index.isValid())
          {
          Line_map[index.row()][index.column()] = value.toString();
          QModelIndex top = createIndex(index.row(), 0);
          QModelIndex bottom = createIndex(index.row(), 3);

          	emit dataChanged(top, bottom);
          	return true;
          }
          return false;
          

          }

          Qt::ItemFlags LineModel::flags(const QModelIndex& index) const
          {
          return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
          }

          This is how I connect the signal and slot

          connect(ui->lineClass, &QComboBox::currentTextChanged, this, &GetOptionsClass::TextChanged);

          Can you please point out what I am doing wrong

          J Online
          J Online
          jeremy_k
          wrote on 12 Dec 2022, 02:38 last edited by
          #4

          @jaivardhanf said in How to change QTableView data based on selection in QComboBox.:

          So below is my code.

          Something seems to have gone wrong with the code tags, making it hard to read and potentially missing code.

          Qt::ItemFlags LineModel::flags(const QModelIndex& index) const
          {
          return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
          }

          Adding the ItemIsEditable flag doesn't matter if the goal isn't to edit the item through the view.

          This is how I connect the signal and slot

          connect(ui->lineClass, &QComboBox::currentTextChanged, this, &GetOptionsClass::TextChanged);

          Verify that the slot is called when it should be, and that the arguments are as expected.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0

          2/4

          11 Dec 2022, 08:03

          • Login

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