Results 1 to 2 of 2

Thread: qtablewidget edit only numbers

  1. #1
    Join Date
    Sep 2014
    Location
    Krusevac, Serbia
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Question qtablewidget edit only numbers

    Hello
    I use tablewidget to presenet data and in one column user can input data but i dont know how to limit user to enter only numbers and of course on end i must get imputed values trought loop.


    Qt Code:
    1. while (query1.next()) {
    2. ui->tableWidget->setItem(i, 0, new QTableWidgetItem(query1.value(0).toString()));
    3. //ui->tableWidget->EditingState;
    4. ui->tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(query1.value(1).toDouble(), 'f', 2)));
    5. ui->tableWidget->setItem(i, 2, new QTableWidgetItem(""));
    6. ui->tableWidget->item(i,0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
    7. ui->tableWidget->item(i,1)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
    8. ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignCenter);
    9. ui->tableWidget->item(i,1)->setTextAlignment(Qt::AlignRight);
    10. ui->tableWidget->item(i,2)->setTextAlignment(Qt::AlignRight);
    11. /*
    12.   QLineEdit * tableline = new QLineEdit;
    13.   tableline->setValidator( new QDoubleValidator(0, 100, 2, this) );
    14.   ui->tableWidget->setCellWidget (i, 2, tableline);
    15.   qDebug() << ui->tableWidget->cellWidget(i,2)->
    16.   */
    17. i++;
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qtablewidget edit only numbers

    The usual way to do this is to create a custom QStyledItemDelegate and install it on the column using QTableView::setItemDelegateForColumn(). In the createEditor() method for your delegate, you would create a QLineEdit -and- install the QDoubleValidator on that.

    Here is a good tutorial on how to do that (by creating a QSpinBox as the custom editor). Beware of the typos. You should find it pretty easy to implement your own delegate from this example.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Item edit in QTableWidget
    By manmohan in forum Newbie
    Replies: 3
    Last Post: 29th April 2011, 17:56
  2. Enter only numbers in the text edit??
    By Gokulnathvc in forum Newbie
    Replies: 8
    Last Post: 23rd March 2011, 19:01
  3. QtableWidget and numbers of row
    By VitaliBR in forum Newbie
    Replies: 8
    Last Post: 4th February 2011, 00:32
  4. Replies: 1
    Last Post: 3rd January 2011, 12:42
  5. Replies: 2
    Last Post: 17th October 2010, 13:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.