July 31, 2012

Vidhya Vidhya
Ant Farmer
48 posts

Set column as not editable in QTableWidget

 

  1. connect(ui->widget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(column_check(int,int)));
  2.  
  3.  
  4. void Saleorder::column_check(int row,int column)
  5. {
  6.     QTableWidgetItem* itemtot=ui->widget->item(row, 9);
  7.  
  8.     if( column == 9 )
  9.     {
  10.         if (itemtot && !itemtot->text().isEmpty())
  11.         {
  12.             itemtot->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
  13.         }
  14.  
  15.     }
  16. }

i tried this but the program is crashed.

Any one help me solve the problem.

Is there is any other way to achieve to make the entire column as not editable

Thanks in advance

 Signature 

Regards,
Vidhya

8 replies

July 31, 2012

DerManu DerManu
Robot Herder
458 posts

Have you tried unsetting Qt::ItemIsEditable on the respective cells on row creation (not on click)?

July 31, 2012

Sam Sam
Area 51 Engineer
628 posts

check this [qt-project.org].

July 31, 2012

Vidhya Vidhya
Ant Farmer
48 posts

S i tried the same code on row creation also

 Signature 

Regards,
Vidhya

July 31, 2012

Sam Sam
Area 51 Engineer
628 posts

Did you get it working ?

July 31, 2012

Vidhya Vidhya
Ant Farmer
48 posts

no its not working . because at first the item is empty

 Signature 

Regards,
Vidhya

July 31, 2012

Vidhya Vidhya
Ant Farmer
48 posts

  1. Qt::ItemFlags Saleorder::flags( const QModelIndex &index) const
  2. {
  3.     Qt::ItemFlags flags = QSqlTableModel::flags(index);
  4.     if (index.column() == 9 )
  5.         flags &= ~Qt::ItemIsEditable;
  6.     return flags;
  7. }

i use this but it displays the below error message.
saleorder.cpp:98: error: cannot call member function ‘virtual Qt::ItemFlags QSqlTableModel::flags(const QModelIndex&) const’ without object

 Signature 

Regards,
Vidhya

July 31, 2012

KA51O KA51O
Robot Herder
397 posts
Vidhya wrote:
i use this but it displays the below error message. saleorder.cpp:98: error: cannot call member function ‘virtual Qt::ItemFlags QSqlTableModel::flags(const QModelIndex&) const’ without object

Read the compiler message.
flags is not a static function and thus needs an object to be called from. So this:

  1. Qt::ItemFlags flags = QSqlTableModel::flags(index);

is not valid code.
If saleorder is derived from QSqlTableModel (which i’d doubt) you can do this:
  1. Qt::ItemFlags flags = flags(index);

otherwise you need a QSqlTableModel object
  1. Qt::ItemFlags flags = someQSqlTableModelObject.flags(index);
  2. //or
  3. Qt::ItemFlags flags = someQSqlTableModelObjectPointer->flags(index);

Maybe this will work in your case:
  1. Qt::ItemFlags flags = ui->widget->model()->flags(index);

July 31, 2012

francomartins francomartins
Ant Farmer
65 posts

try it :

// Qt::noItemFlags disable all flags , and u can add the flags again ;

  1. void Saleorder::column_check(int row,int column)
  2. {
  3.  if (!ui->widget->item(row, 9)->text().isEmpty())
  4.     {
  5. ui->widget->item(row,9)->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
  6.     }
  7. }

 
  ‹‹ [SOLVED] Apply general shader on whole QGLPainter      Help with changing QScrollArea behavior. ››

You must log in to post a reply. Not a member yet? Register here!