Results 1 to 5 of 5

Thread: QRadioButtonTableItem like QCheckTableItem

  1. #1
    Join Date
    Jan 2006
    Location
    India
    Posts
    54
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Question QRadioButtonTableItem like QCheckTableItem

    Actually I want to implement a QRadioButton as a QTableItem (like QCheckTableItem).
    I create the radio button is QTableItem’s createEditor() function.

    But my problems are:
    1. How do I handle signal and slots, since QTableItem is inherited from Qt so I cant define signal and slots in it. What should be the approach?
    2. When I try to show the radio buttons on 3*3 table onle one radio button appear at 0,0 position and other appears when I click any othe cell. How do I show them?

    thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QRadioButtonTableItem like QCheckTableItem

    Answer 1 : use QRadioButton pointer(declare it as member of your QTableItem class) , and then operate with it ;
    Answer 2 : show your code ;
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Location
    India
    Posts
    54
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QRadioButtonTableItem like QCheckTableItem

    sradiobuttontableitem.h
    Qt Code:
    1. #ifndef _S_RADIO_BUTTON_TABLE_ITEM_H_
    2. #define _S_RADIO_BUTTON_TABLE_ITEM_H_
    3.  
    4. #include <qtable.h>
    5.  
    6. class SRadioButtonTableItem;
    7.  
    8. class STable : public QTable
    9. {
    10. Q_OBJECT
    11. public:
    12. STable(int row, int col, QWidget *parent = 0, const char *name = 0);
    13. ~STable();
    14.  
    15. SRadioButtonTableItem *item(int, int);
    16. public slots:
    17. void doValueChanged();
    18. void clicked(int, int);
    19. };
    20.  
    21. class SRadioButtonTableItem : public QTableItem
    22. {
    23. public:
    24. SRadioButtonTableItem(STable *table, const QString &text);
    25. virtual ~SRadioButtonTableItem();
    26.  
    27. bool isChecked() const { return m_bChecked; }
    28. bool isBold() const { return m_bBold; }
    29.  
    30. void setChecked(bool check) { m_bChecked = check; }
    31. void setBold (bool boldToggle) { m_bBold = boldToggle; }
    32.  
    33. protected:
    34. virtual QWidget *createEditor() const;
    35. virtual void setContentFromEditor ( QWidget * w );
    36. virtual void paint (QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected);
    37.  
    38. private:
    39. const QString m_string;
    40. bool m_bBold;
    41. bool m_bChecked;
    42. };
    43.  
    44. #endif
    To copy to clipboard, switch view to plain text mode 

    sradiobuttontableitem.cpp
    Qt Code:
    1. #include <qradiobutton.h>
    2. #include <qmessagebox.h>
    3. #include <qcolor.h>
    4. #include <qpainter.h>
    5. #include <assert.h>
    6.  
    7. #include "sradiobuttontableitem.h"
    8.  
    9. STable::STable(int row, int col, QWidget *parent, const char *name)
    10. : QTable(row, col, parent, name) {
    11.  
    12. QObject::connect(this, SIGNAL(valueChanged(int, int)), this, SLOT(clicked(int, int)));
    13. }
    14.  
    15. void STable::clicked(int row, int col)
    16. {
    17. if(item(row, col)->isChecked()) {
    18. QMessageBox::information(this, 0, "TRUE");
    19. } else {
    20. QMessageBox::information(this, 0, "FLASE");
    21. }
    22. }
    23.  
    24. SRadioButtonTableItem* STable::item(int row, int col)
    25. {
    26. SRadioButtonTableItem *ti = (SRadioButtonTableItem *)QTable::item(row,col);
    27. return ti;
    28. }
    29.  
    30. STable::~STable() {
    31. }
    32.  
    33. void STable::doValueChanged() {
    34. emit valueChanged(currentRow(), currentColumn());
    35. }
    36.  
    37. SRadioButtonTableItem::SRadioButtonTableItem(STable *table, const QString &text)
    38. : QTableItem( table, QTableItem::WhenCurrent, text ),
    39. m_string(text),
    40. m_bBold(FALSE),
    41. m_bChecked(FALSE) {
    42. setReplaceable( false );
    43. }
    44.  
    45. SRadioButtonTableItem::~SRadioButtonTableItem() {
    46. }
    47.  
    48. QWidget* SRadioButtonTableItem::createEditor() const {
    49. QWidget* editorWidget;
    50. STable *_table = static_cast<STable*>(table());
    51.  
    52. editorWidget = new QRadioButton(table()->viewport() );
    53.  
    54. ((QRadioButton*)editorWidget)->setText( text() );
    55.  
    56. QObject::connect(editorWidget, SIGNAL(clicked()), _table, SLOT(doValueChanged()));
    57. return editorWidget;
    58. }
    59.  
    60. void SRadioButtonTableItem::setContentFromEditor ( QWidget * w )
    61. {
    62. if ( w )
    63. {
    64. ((QRadioButton*)w)->show();
    65. if(((QRadioButton*)w)->isChecked()) {
    66. m_bChecked = TRUE;
    67. } else {
    68. m_bChecked = FALSE;
    69. }
    70. }
    71. }
    72.  
    73. void SRadioButtonTableItem::paint (QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected)
    74. {
    75. QFont f = p->font();
    76.  
    77. f.setBold (m_bBold);
    78. p->setFont (f);
    79.  
    80. QTableItem::paint (p, cg, cr, selected);
    81. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qstring.h>
    3. #include <qtable.h>
    4.  
    5. #include "sradiobuttontableitem.h"
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QApplication qApp(argc, argv);
    10.  
    11. STable *table = new STable(3, 3);
    12.  
    13. for(int row = 0; row < 3; ++row) {
    14. for(int col = 0; col < 3; ++col) {
    15. QString str = "TEST";
    16. table->setItem(row, col, new SRadioButtonTableItem(table, str));
    17. }
    18. }
    19.  
    20. qApp.setMainWidget(table);
    21.  
    22. table->show();
    23.  
    24.  
    25. qApp.connect(&qApp, SIGNAL(lastWindowClosed()), &qApp, SLOT(quit()));
    26.  
    27. return qApp.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    India
    Posts
    54
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QRadioButtonTableItem like QCheckTableItem

    Hi

    Now I have implement the communication functionality, but now my problem is that my radio button does not appear always as I have used the EditType is WhenCurrent.

    I used this edit type because QCheckTableItem use this edit type.

    If I use always edit type the radio button appear always but my application crashes when I click on the radio botton.

    I want my radio button warks exactly as the QCheckBoxTableItem.

    thanks.

  5. #5
    Join Date
    Jan 2006
    Location
    India
    Posts
    54
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QRadioButtonTableItem like QCheckTableItem

    Can anybody help me out for issue!

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.