Results 1 to 15 of 15

Thread: QTableWidget combobox

  1. #1
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QTableWidget combobox

    Dear All

    We have a problem for the QTableWidget.

    We have an widget that containes combo boxes in some of the column. What we would like to do is, when we start editing a column it should turn into a combo box, and after we finish, it should turn into a normal qtablewidget item.

    Please help us

  2. #2
    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 combobox

    Take a look at the spinbox delegate example. Provide a combo box editor in the similar way than the example provides a spinbox.
    J-P Nurmi

  3. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget combobox

    You dont keep combo boxes in your QTableWidget initially.
    keep one combo boxe pointer seperatally as a member of class with hide.

    whenever you click in any cell you do the following:
    1. initialize your combo box with your data.
    2. take data of that tableWidget's selected cell data and match with combo box data to make it selected in combobox
    3. set combo box item in selected cell and show
    4. after editing you read data of combo box and place in cell
    5. hide your combo box.

  4. #4
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    Dear jpn

    For the spin box delegate doesnt help, because the data in the combo changes allways, but the other solution could help

    Like the combos should be in the 2nd coloum. how we could send a signal for the second coloum?

  5. #5
    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 combobox

    Quote Originally Posted by aekilic View Post
    Like the combos should be in the 2nd coloum.
    You can create combo box editors to 2nd column like this:
    Qt Code:
    1. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. if (index.column() == 1)
    6. {
    7. // 2nd column is being edited, return a QComboBox as an editor
    8. QComboBox *editor = new QComboBox(parent);
    9. ...
    10. return editor;
    11. }
    12. return QItemDelegate::createEditor(parent, option, index);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by aekilic View Post
    For the spin box delegate doesnt help, because the data in the combo changes allways
    Sure it does. setEditorData() gets always called for the item in question. Just fill the combo box accordingly:
    Qt Code:
    1. void ComboBoxDelegate::setEditorData(QWidget *editor,
    2. const QModelIndex &index) const
    3. {
    4. if (index.column() == 1)
    5. {
    6. // 2nd column is being edited, we know it's a QComboBox
    7. // fill the combo box with anything you want
    8. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    9. comboBox->addItem(...);
    10. ...
    11. }
    12. else
    13. {
    14. QItemDelegate::setEditorData(editor, index);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    aekilic (14th September 2007)

  7. #6
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    Dear Jpn

    Could you please help us how we are going to put these into our code? Do we have to write a new cpp?

  8. #7
    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 combobox

    Yes, I'd suggest implementing the delegate into separate files. Could you take a look at the spinbox delegate example as already suggested? Just create similar files for the delegate.
    J-P Nurmi

  9. #8
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    we have made a new .h and a new .cpp

    like
    Qt Code:
    1. #ifndef TABLEIHALESARTNAMEDELEGATE_H
    2. #define TABLEIHALESARTNAMEDELEGATE_H
    3.  
    4. #include <QItemDelegate>
    5. #include <QModelIndex>
    6. #include <QObject>
    7. #include <QSize>
    8. #include <QDoubleSpinBox>
    9. #include <QtGui>
    10.  
    11.  
    12. class ComboBoxDelegate : public QItemDelegate
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. ComboBoxDelegate(QObject *parent = 0);
    18.  
    19. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    20. const QModelIndex &index) const;
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    and made our .cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "tableIhaleSartnameDelegate.h"
    3.  
    4. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    5. const QStyleOptionViewItem &option,
    6. const QModelIndex &index) const
    7. {
    8. if (index.column() == 0)
    9. {
    10. // 2nd column is being edited, return a QComboBox as an editor
    11. QComboBox *editor = new QComboBox(parent);
    12.  
    13. return editor;
    14. }
    15. return QItemDelegate::createEditor(parent, option, index);
    16. }
    To copy to clipboard, switch view to plain text mode 

    after that what we should do?

  10. #9
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    and how are are going to call for our delegate?

  11. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    Now you have to use it.
    Qt Code:
    1. QComboBox *combo = new QComboBox();
    2. combo->setItemDelegate(new ComboBoxDelegate());
    To copy to clipboard, switch view to plain text mode 


    You could have seen how to use it if you just looked at the delegate examples.

  12. The following user says thank you to marcel for this useful post:

    aekilic (14th September 2007)

  13. #11
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    I have tried that but we have an error like

    QtCore -lz -lm -lrt -ldl -lpthread
    obj/moc_anaPencere.o: In function `formIhale::ihaleSartnameGoster()':
    moc_anaPencere.cpp.gnu.linkonce.t._ZN9formIhale19ihaleSartnameGoster Ev+0x4d5): undefined reference to `ComboBoxDelegate::ComboBoxDelegate(QObject*)'
    collect2: ld returned 1 exit status
    make: *** [Deneme34d] Hata 1

  14. #12
    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 combobox

    Quote Originally Posted by aekilic View Post
    I have tried that but we have an error like

    QtCore -lz -lm -lrt -ldl -lpthread
    obj/moc_anaPencere.o: In function `formIhale::ihaleSartnameGoster()':
    moc_anaPencere.cpp.gnu.linkonce.t._ZN9formIhale19ihaleSartnameGoster Ev+0x4d5): undefined reference to `ComboBoxDelegate::ComboBoxDelegate(QObject*)'
    collect2: ld returned 1 exit status
    make: *** [Deneme34d] Hata 1
    Implementation body is missing. In another words, there's a declaration but no implementation for ComboBoxDelegate constructor.
    J-P Nurmi

  15. The following user says thank you to jpn for this useful post:

    aekilic (17th September 2007)

  16. #13
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    What should we do?

  17. #14
    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 combobox

    Plain declaration of a function is not enough. To be able to use a function you must also implement it. So add an implementation for ComboBoxDelegate constructor to your .cpp file:
    Qt Code:
    1. ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
    2. QItemDelegate(parent)
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  18. The following user says thank you to jpn for this useful post:

    aekilic (17th September 2007)

  19. #15
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget combobox

    Dear All

    Thank you very much we have solved our problem. We were able to delegate the combobox!

Similar Threads

  1. Qtablewidget Combobox
    By aekilic in forum Qt Programming
    Replies: 17
    Last Post: 7th July 2015, 11:22
  2. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 19th December 2006, 23:27
  3. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  4. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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.