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. QTableView: Checkbox is not editable

QTableView: Checkbox is not editable

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.9k 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.
  • A Offline
    A Offline
    AchimTr
    wrote on last edited by AchimTr
    #1

    Hi,

    i´m trying to show a Checkbox in my QTableView ( instead of the value ( 0 / 1 ) of the database ). Therefore i´ve written my own Model, in which i´ve overwritten flags(), setdata() and data():

    The Checkbox is shown, but is not editable, nothing happens when i click on the Box. Can you help me here? I dont understand why.

    Regards
    Achim

    Qt::ItemFlags CinemaModel::flags(const QModelIndex &index) const
    {
    	if (index.column() == 4)
    	{
    	return  Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
    	}
    
    	return QSqlQueryModel::flags(index);
    }
    
    
    QVariant CinemaModel::data(const QModelIndex & index, int role) const
    {
    	if( index.column() == 4 && role == Qt::CheckStateRole )
    	{
    		if (role == Qt::CheckStateRole)
    		{
    			return index.data(Qt::EditRole).toBool() ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
    		}		
    	}
    	return QSqlQueryModel::data(index, role);
    }
    
    
    bool CinemaModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    	if (index.column() == 4)
    	{
    		QVariant data = value.toInt() == Qt::Checked ? QVariant(true) : QVariant(false);
    		return QSqlQueryModel::setData(index, data, Qt::EditRole);
    	}
    	
    	return QSqlQueryModel::setData(index, value, role);	
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      AFAIR, with the set of flags you return, your item will not be editable.

      You should rather have something like:

      Qt::ItemFlags CinemaModel::flags(const QModelIndex &index) const 
      {
          Qt::ItemFlags result = QSqlTableModel::flags(index);
          if (index.column() == 4) {
              result |= Qt::ItemIsUserCheckable;
          }
          return result;
      }
      

      Note that QSqlQueryModel is a read-only model so you may have to also add the Qt::ItemIsEditable flag.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • A Offline
        A Offline
        AchimTr
        wrote on last edited by
        #3

        THX SGaist for the hint with the read-only model, i didn´t noticed it. Now, i use a QSqlTableModel instead of a QueryModel and it woks, while using the right flags...

        Only one problem left...I must doubeblick on the cell with the checkbox, so the checkbox gets the focus. And then i can can check/uncheck it. Do you / anyone know a solution, with which i can change the state with only one click? I´ve tried already to ovveride the mousePressEvent(), but iz didnt worked :/

        Regards
        Achim

        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