Results 1 to 3 of 3

Thread: tableview with doubles

  1. #1
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: tableview with doubles

    Hello,

    I want a tableView with doubles, for instance:

    QVector<double> data(10);
    for (int i=0;i<10;++i)
    data[i]=1+i/10;
    QStandardItemModel *model = new QStandardItemModel(10,2,this);
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column 1")));
    ...
    for(int row=0;row<10;++row)
    for(int col=0;col<2;++col)
    {
    QModelIndex index = model -> index(row,col,QModelIndex());
    model->setData(index,data[row]);
    }
    But I get integers in the table, how can I get doubles: 1.1

    Regards,
    Arend


    Added after 12 minutes:


    I have solved this one
    model->setData(index(row,column),QString::number(yourNum ber, 'f', 3));
    Last edited by Arend; 25th November 2012 at 21:56.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: tableview with doubles

    I have solved this one
    model->setData(index(row,column),QString::number(yourNum ber, 'f', 3));
    No, actually that puts strings (wrapped in QVariant) into the model. For a lot of purposes this may be adequate. If you want an actual QVariant<double> to preserve precision then you need to use the setData() function with a double. You can use a delegate to drive the display form of the double. Compare these:
    Qt Code:
    1. double yourNumber = 10.7002;
    2.  
    3. item.setData(QString::number(yourNumber, 'f', 3), Qt::DisplayRole);
    4. qDebug() << item.data(Qt::DisplayRole) << item.data(Qt::DisplayRole).toDouble();
    5. qDebug() << item.data(Qt::DisplayRole);
    6. // QVariant(QString, "10.700") 10.7
    7.  
    8. item.setData(yourNumber, Qt::DisplayRole);
    9. qDebug() << item.data(Qt::DisplayRole) << item.data(Qt::DisplayRole).toDouble();
    10. // QVariant(double, 10.7002) 10.7002
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: tableview with doubles

    Quote Originally Posted by Arend View Post
    Qt Code:
    1. QVector<double> data(10);
    2. for (int i=0;i<10;++i)
    3. data[i]=1+i/10;
    To copy to clipboard, switch view to plain text mode 
    Hmm... let me see.... data[0] = 1, data[1] = 1, data[2] = 1, data[3] = 1, data[4] = 1 ... data[9] = 1

    They are all integers.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. how to present a vector of doubles
    By Arend in forum Newbie
    Replies: 1
    Last Post: 25th November 2012, 19:49
  2. QTableView and display of doubles
    By b1 in forum Qt Programming
    Replies: 9
    Last Post: 18th November 2011, 22:52
  3. vector of doubles to QByteArray
    By gmseed in forum Qt Programming
    Replies: 5
    Last Post: 16th October 2009, 15:09
  4. QTextStream, doubles and ints
    By SirhcdP in forum Qt Programming
    Replies: 4
    Last Post: 18th March 2009, 11:13
  5. QTreeView questions about view doubles and more
    By davisjamesf in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 00:39

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.