Results 1 to 5 of 5

Thread: Creatin an editable array

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Creatin an editable array

    Hi,

    First of all, english isn't my mothertongue so I want to apologize for any mistake that could be present in this post.

    I'm kind of new in Qt (I'm using Qt 4.8.2 by the way) and I would like to create an array similar to the QTableView object.
    However, I want that each row of the two first column of this array to be editable.

    For example, let's say that the user can enter any integer he wants in the two first column and the third one will contain their sum.
    The user can't alter the data contained in the third column.

    I've searched on the Qt documentation but I didn't found anything that allow to create this kind of array.

    Thanks for answering me

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Creatin an editable array

    Perhaps QTableWidget.

    With it, you can make parts of the table read-only and others editable, see for example
    http://stackoverflow.com/questions/2...dget-read-only

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Creatin an editable array

    The most proper approach is to use QTableView and a custom model with reimplemented methods such as QAbstractItemModel::setData() and QAbstractItemModel::flags(). Sum calculation would take place when setData() is called and flags() allow you to control which cells are editable and which are not.
    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.


  4. #4
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creatin an editable array

    Hi,

    Thanks for your answers,
    I used the following code:
    Qt Code:
    1. void test::disableEdit(QTableWidgetItem* pItem)
    2. {
    3. Qt::ItemFlags eFlags = pItem->flags();
    4. eFlags &= ~Qt::ItemIsEditable;
    5. pItem->setFlags(eFlags);
    6. }
    7.  
    8. void test::initArray()
    9. {
    10. this->arr = new QTableWidget();
    11. int nrow, ncolumn;
    12. nrow = 10;
    13. ncolumn = 3;
    14. this->arr->setRowCount(nrow);
    15. this->arr->setColumnCount(ncolumn);
    16. list << "Col 1" << "Col 2" << "Col 3";
    17. this->arr->setHorizontalHeaderLabels(list);
    18. int i = 0;
    19. while (i <= arr->rowCount())
    20. {
    21. this->disableEdit(newitem);
    22. this->arr->setItem(i, 2, newitem); // 2 is the number of the column on which I disable edition
    23. ++i;
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    It works but I have to loop for every column when I want to disable edition which is kind of long.

    @wysota: I understand what you said but I have no idea on how to use a custom model and QAbstractItemModel::flags() to control which cells are editable and which are not.
    Do you have a link to a tutorial or some simple code sample?

    Thanks for your answers.
    Last edited by Glouglou; 2nd July 2012 at 19:13.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Creatin an editable array

    All you need is in the docs. flags() should check the column one is asking about and either include Qt::ItemIsEditable in the return value or not based on whether you want a particular column to be editable or not.
    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. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 08:28
  2. Replies: 2
    Last Post: 12th November 2010, 14:42
  3. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  4. editable rectangles
    By parmar ranjit in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2008, 09:59
  5. Creatin Key Pad shortcuts
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 19th May 2006, 05:50

Tags for this Thread

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.