Page 1 of 3 123 LastLast
Results 1 to 20 of 54

Thread: QTableWidget problem

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QTableWidget problem

    Hi to all!

    I need to develop an subclassed table widget, which will look something like that:
    Qt Code:
    1. +===================================+
    2. + Coca Cola +
    3. + 1,50 EUR 002 3,00 EUR +
    4. + Sprite +
    5. + 1,30 EUR 010 13,00 EUR +
    6. + Cheeseburger with salad +
    7. + 3,80 EUR 004 15,20 EUR +
    8. + Beefstake with potato and salad +
    9. + 24,67 EUR 012 296,04 EUR +
    10. +===================================+
    11. + TOTAL: 327,24 EUR +
    12. +===================================+
    To copy to clipboard, switch view to plain text mode 
    The point is that every row consists of two lines, the upper one will hold merchandize name (solo column) and the lower one will consists of following fields in order:
    • price/merchandize unit (eur formatted, including euro sign
    • number of merchandize unit (formatted with leading 0)
    • price/unit*number of merchandize units (eur formatted, including euro sign)
    • at the end there will be total field

    Does someone has any idea how to achieve that?!
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    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: QTableWidget problem

    Have you already tried anything?

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    No, I need a few hints how to make two lines in a cell. Then I will begin.
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    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: QTableWidget problem

    Add a newline character to the string displayed.

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    Ok, I am not total idiot. I need a cell, which is constructed from:
    • upper string line
    • and lower line, which is constructed from
      • price/unit string line and eur icon - left formatted
      • quantity string line (with leading zeros)
      • price/unit*quantity string line and eur icon - right formatted
    I cannot solve this with newline character.
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget problem

    Well, why cant u have the brand name as column ? why need it as row ??

    I am not sure of this will work... but hers my idea..
    U can use delegates to have each cell as a widget. This widget has two rows... first the name, second the pprice, quantity etc.

    hope it helps

  7. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    I am limited with table's width, it is critical to minimize table's width because the neighbour widget must take the largest size possible because of data representation.
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget problem

    So, what prevents you from using multiple columns? You'll get nice alignment out of the box. Besides, I'd use tree view instead of table view.
    J-P Nurmi

  9. #9
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTableWidget problem

    You can create a layout on a widget wich has the strings that you said and icons and then set this widget as a cell widget. Though it can be slow...

    The other way is to implement a delegate and draw your cell there
    C++ & AMD forever

  10. #10
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    Quote Originally Posted by jpn View Post
    So, what prevents you from using multiple columns? You'll get nice alignment out of the box. Besides, I'd use tree view instead of table view.
    Tree view is clumsy for touch screen user interaction. I've tried it before and because of fu... users I had to recode tree view into representation I want to do now in Qt. The same solution worked in Clarion for Windows.

    Quote Originally Posted by THRESHE View Post
    You can create a layout on a widget wich has the strings that you said and icons and then set this widget as a cell widget. Though it can be slow...

    The other way is to implement a delegate and draw your cell there
    So. thanks for guideline, I will get to read docs about delegates ASAP.
    Qt 5.3 Opensource & Creator 3.1.2

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    Well, now I have developec merchandize order class. What do I need to do to get the requested form in delegate? Reimplement the paintEvent()?? Or what?
    Qt 5.3 Opensource & Creator 3.1.2

  12. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    Well, I am slowly managing to get somewhere with my problem. Now, how do I translate the table cell (which has QString, qreal, qint16 and another qreal) from the first post into QVariant type? And how do I transfrom this cell into single QString?
    Qt 5.3 Opensource & Creator 3.1.2

  13. #13
    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: QTableWidget problem

    If you use QTableWidget then you have to iterate all the cells and translate them manually. With a model based approach you could embed that functionality in the model.

  14. #14
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    I've taken a look at Pixelator example and built my own model based on ImageModel class. Now, how do I embedd this functionality into my class, which code is:
    Qt Code:
    1. #ifndef CSHOPPINGCARTMODEL_H_
    2. #define CSHOPPINGCARTMODEL_H_
    3.  
    4. // qt includes
    5. #include <QAbstractTableModel>
    6. #include <QList>
    7. #include <QString>
    8. #include <QSize>
    9.  
    10. // custom includes
    11. //#include "CMerchandizeOrder.h"
    12. //#include "globals.h"
    13.  
    14. typedef struct
    15. {
    16. qint16 iMerchandizeID;
    17. QString strMerchandizeName;
    18. qreal rMerchandizePrice;
    19. qint16 iMerchandizeQuantity;
    20. qreal rSubtotal;
    21. } structOrder;
    22.  
    23. typedef QList<structOrder> structOrders;
    24.  
    25. class CShoppingCartModel : public QAbstractTableModel
    26. {
    27. Q_OBJECT
    28.  
    29. public:
    30. CShoppingCartModel(QObject* parent=0);
    31. void setData(const structOrders& merchandizeOrders);
    32.  
    33. int rowCount(const QModelIndex& parent=QModelIndex()) const;
    34. int columnCount(const QModelIndex& parent=QModelIndex()) const;
    35.  
    36. QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
    37. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    38.  
    39. private:
    40. structOrders m_ModelData;
    41. };
    42.  
    43. #endif /*CSHOPPINGCARTMODEL_H_*/
    To copy to clipboard, switch view to plain text mode 
    and it's implementation:
    Qt Code:
    1. #include "CShoppingCartModel.h"
    2.  
    3. CShoppingCartModel::CShoppingCartModel(QObject* parent)
    4. {
    5. }
    6.  
    7. void CShoppingCartModel::setData(const structOrders& merchandizeOrder)
    8. {
    9. m_ModelData=merchandizeOrder;
    10. reset();
    11. }
    12.  
    13. int CShoppingCartModel::rowCount(const QModelIndex& /* parent */) const
    14. {
    15. return m_ModelData.size();
    16. }
    17.  
    18. int CShoppingCartModel::columnCount(const QModelIndex& /* parent */) const
    19. {
    20. return 1;
    21. }
    22.  
    23. QVariant CShoppingCartModel::data(const QModelIndex& index, int role) const
    24. {
    25. // invalid order setup
    26. structOrder invalidOrder;
    27. invalidOrder.iMerchandizeID=0;
    28. invalidOrder.iMerchandizeQuantity=0;
    29. invalidOrder.rMerchandizePrice=0.00;
    30. invalidOrder.rSubtotal=0.00;
    31. invalidOrder.strMerchandizeName=QString("");
    32.  
    33. /*
    34.   if (!index.isValid() || role!=Qt::DisplayRole)
    35.   return invalidOrder;
    36.   //return qGray(modelImage.pixel(index.column(), index.row()));
    37.   return m_ModelData.at(index.column());
    38. */
    39. return QVariant();
    40. }
    41.  
    42. QVariant CShoppingCartModel::headerData(int /* section */,
    43. Qt::Orientation /* orientation */,
    44. int role) const
    45. {
    46. if (role==Qt::SizeHintRole)
    47. return QSize(1, 1);
    48. return QVariant();
    49. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, I do not know how to return whole record as QVariant.
    Qt 5.3 Opensource & Creator 3.1.2

  15. #15
    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: QTableWidget problem

    Quote Originally Posted by MarkoSan View Post
    As you can see, I do not know how to return whole record as QVariant.
    Why would you want to do that?

    BTW. Don't call reset() from setData(). emit dataChanged() instead.

  16. #16
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    Well, from the beggining of this thread you saw I need a table with 1 column and orders.size() columns. For data cell I will implement my own delegate, which will represent one cell containing of:
    Qt Code:
    1. +=====================================================================+
    2. + SPRITE 0,33L (QString) +
    3. + 2,55 EUR (qreal) 3x (qint16) 7,65 EUR (qreal) +
    4. +---------------------------------------------------------------------+
    5. + PIZZA PARMA (QString) +
    6. + 12,23 EUR (qreal) 2x (qint16) 24,46 EUR (qreal) +
    7. +=====================================================================+
    8. TOTAL: 32,11 EUR (qreal)
    To copy to clipboard, switch view to plain text mode 
    So as I understood, every cell is QVariant and every QVariant then will contain the values form QList of
    Qt Code:
    1. typedef struct
    2. {
    3. qint16 iMerchandizeID;
    4. QString strMerchandizeName;
    5. qreal rMerchandizePrice;
    6. qint16 iMerchandizeQuantity;
    7. qreal rSubtotal;
    8. } structOrder;
    To copy to clipboard, switch view to plain text mode 
    So, I need a mechanismus to serialize all this info into one QVariant. Am I wrong?
    Qt 5.3 Opensource & Creator 3.1.2

  17. #17
    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: QTableWidget problem

    Yes, you are wrong You need a custom delegate that will render the data fetched from a single item using different item roles (like the text, icon, etc. - your custom roles included).

    And to be honest, I'd do it completely differently. I'd have a table with three columns and simply span appropriate rows across all columns.

  18. #18
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    Yes, you are wrong You need a custom delegate that will render the data fetched from a single item using different item roles (like the text, icon, etc. - your custom roles included).
    So, I can use existing model classes, existing view classes and I only reimplement delegate class?

    And to be honest, I'd do it completely differently. I'd have a table with three columns and simply span appropriate rows across all columns.
    The problem is that this table must be wide as little as possible, because on 17" touch monitor I am very limited with its width. About 90% of desktop (from 17") there is a some operation window. But because your ideas helped me a lot, can you provide me some screenshot, what did you mean by quote?
    Qt 5.3 Opensource & Creator 3.1.2

  19. #19
    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: QTableWidget problem

    Quote Originally Posted by MarkoSan View Post
    So, I can use existing model classes, existing view classes and I only reimplement delegate class?
    Yes, that's correct. QStandardItemModel has everything you might need.

    The problem is that this table must be wide as little as possible, because on 17" touch monitor I am very limited with its width. About 90% of desktop (from 17") there is a some operation window. But because your ideas helped me a lot, can you provide me some screenshot, what did you mean by quote?
    I don't have a screenshot. I meant to use QTableView::setSpan().

  20. The following user says thank you to wysota for this useful post:

    MarkoSan (9th April 2008)

  21. #20
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTableWidget problem

    But, which delegate should I use, QItemDelegate or QAbstractItemDelegate? At this moment, I am totaly confused ...
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Replies: 3
    Last Post: 11th August 2007, 11:00
  2. experiencing problem at printing out QTableWidget
    By rmagro in forum Qt Programming
    Replies: 9
    Last Post: 27th June 2007, 17:04
  3. QTableWidget Problem, setRowWidget? :P
    By VireX in forum Newbie
    Replies: 17
    Last Post: 6th April 2007, 19:12
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 19:46
  5. Problem inserting in QTableWidget
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 01:10

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.