PDA

View Full Version : QRadioButtonTableItem like QCheckTableItem



sumsin
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?

thanks.

zlatko
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 ;

sumsin
20th June 2006, 11:46
sradiobuttontableitem.h


#ifndef _S_RADIO_BUTTON_TABLE_ITEM_H_
#define _S_RADIO_BUTTON_TABLE_ITEM_H_

#include <qtable.h>

class SRadioButtonTableItem;

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

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

class SRadioButtonTableItem : public QTableItem
{
public:
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; }

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

private:
const QString m_string;
bool m_bBold;
bool m_bChecked;
};

#endif



sradiobuttontableitem.cpp


#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_string(text),
m_bBold(FALSE),
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 )
{
((QRadioButton*)w)->show();
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);
}



main.cpp


#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.setMainWidget(table);

table->show();


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

return qApp.exec();
}

sumsin
20th June 2006, 15:47
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.

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