View Full Version : QRadioButtonTableItem like QCheckTableItem

20th June 2006, 11:26
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?


20th June 2006, 11:34
Answer 1 : use QRadioButton pointer(declare it as member of your QTableItem class) , and then operate with it ;
Answer 2 : show your code ;

20th June 2006, 11:46


#include <qtable.h>

class SRadioButtonTableItem;

class STable : public QTable
STable(int row, int col, QWidget *parent = 0, const char *name = 0);

SRadioButtonTableItem *item(int, int);
public slots:
void doValueChanged();
void clicked(int, int);

class SRadioButtonTableItem : public QTableItem
SRadioButtonTableItem(STable *table, const QString &text);
virtual ~SRadioButtonTableItem();

bool isChecked() const { return m_bChecked; }
bool isBold() const { return m_bBold; }

void setChecked(bool check) { m_bChecked = check; }
void setBold (bool boldToggle) { m_bBold = boldToggle; }

virtual QWidget *createEditor() const;
virtual void setContentFromEditor ( QWidget * w );
virtual void paint (QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected);

const QString m_string;
bool m_bBold;
bool m_bChecked;



#include <qradiobutton.h>
#include <qmessagebox.h>
#include <qcolor.h>
#include <qpainter.h>
#include <assert.h>

#include "sradiobuttontableitem.h"

STable::STable(int row, int col, QWidget *parent, const char *name)
: QTable(row, col, parent, name) {

QObject::connect(this, SIGNAL(valueChanged(int, int)), this, SLOT(clicked(int, int)));

void STable::clicked(int row, int col)
if(item(row, col)->isChecked()) {
QMessageBox::information(this, 0, "TRUE");
} else {
QMessageBox::information(this, 0, "FLASE");

SRadioButtonTableItem* STable::item(int row, int col)
SRadioButtonTableItem *ti = (SRadioButtonTableItem *)QTable::item(row,col);
return ti;

STable::~STable() {

void STable::doValueChanged() {
emit valueChanged(currentRow(), currentColumn());

SRadioButtonTableItem::SRadioButtonTableItem(STabl e *table, const QString &text)
: QTableItem( table, QTableItem::WhenCurrent, text ),
m_bChecked(FALSE) {
setReplaceable( false );

SRadioButtonTableItem::~SRadioButtonTableItem() {

QWidget* SRadioButtonTableItem::createEditor() const {
QWidget* editorWidget;
STable *_table = static_cast<STable*>(table());

editorWidget = new QRadioButton(table()->viewport() );

((QRadioButton*)editorWidget)->setText( text() );

QObject::connect(editorWidget, SIGNAL(clicked()), _table, SLOT(doValueChanged()));
return editorWidget;

void SRadioButtonTableItem::setContentFromEditor ( QWidget * w )
if ( w )
if(((QRadioButton*)w)->isChecked()) {
m_bChecked = TRUE;
} else {
m_bChecked = FALSE;

void SRadioButtonTableItem::paint (QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected)
QFont f = p->font();

f.setBold (m_bBold);
p->setFont (f);

QTableItem::paint (p, cg, cr, selected);


#include <qapplication.h>
#include <qstring.h>
#include <qtable.h>

#include "sradiobuttontableitem.h"

int main(int argc, char **argv)
QApplication qApp(argc, argv);

STable *table = new STable(3, 3);

for(int row = 0; row < 3; ++row) {
for(int col = 0; col < 3; ++col) {
QString str = "TEST";
table->setItem(row, col, new SRadioButtonTableItem(table, str));



qApp.connect(&qApp, SIGNAL(lastWindowClosed()), &qApp, SLOT(quit()));

return qApp.exec();

20th June 2006, 15:47

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.


25th April 2007, 14:55
Can anybody help me out for issue!