PDA

View Full Version : Problem with custom widget



jnana
15th March 2006, 12:25
Hi ,

I am trying to customized QTableWidget Item in Qt-4.1 version(linux). It is compiling
properly and creating Libraries. When instatiating the plugin widgets instead of my class object, it is creating object of base class(QTableWidget). I am not getting the error position. So asking for ur help. Attaching the code.

//spreadsheet.h

#ifndef SPREADSHEET
#define SPREADSHEET
#include <QApplication>
#include <Qt3Support>
#include <QTableWidget>
#include <Q3PopupMenu>
#include <QAction>
#include <QColorDialog>

class SpreadSheet : public QTableWidget {
//Q_OBJECT
public :
SpreadSheet (QWidget* a_parent = 0);
SpreadSheet (int a_row = 1, int a_column = 4,
QWidget *a_parent = 0);
void CreatePopupMenu();
void ChooseColor ();
void ExecPopupMenu(int, int);
private:
unsigned row;
unsigned column;
Q3PopupMenu *menu;
QAction *colorBackAct;
QAction *colorTextAct;
QColor color;
};

#endif

//spreadsheet.cpp
#include "spreadsheet.h"

SpreadSheet :: SpreadSheet (QWidget* a_parent)
:QTableWidget(a_parent)
{
row = 0;
column = 0;
CreatePopupMenu();
}

SpreadSheet :: SpreadSheet (int a_row, int a_column, QWidget* a_parent)
: QTableWidget(a_row, a_column, a_parent)
{
row = a_row;
column = a_column;
CreatePopupMenu();
}

void
SpreadSheet :: CreatePopupMenu()
{
connect(this, SIGNAL(cellDoubleClicked(int, int)),
this, SLOT(ExecPopupMenu(int, int)));

menu = new Q3PopupMenu(this);
menu->addAction("Cell Background color ... ", this, SLOT(ChooseColor()));
menu->addAction("Cell Text Color ...", this, SLOT(ChooseColor()));
}

void
SpreadSheet :: ChooseColor()
{
color = QColorDialog::getColor();
}


void
SpreadSheet :: ExecPopupMenu(int a_row, int a_column)
{
menu->exec();
// QWidget* widget = cellWidget(a_row, a_column);
QTableWidgetItem *item = takeItem(a_row, a_column);
item->setBackgroundColor (color);
}

//customwidgetplugin.h
#ifndef CUSTOMWIDGETPLUGIN_H
#define CUSTOMWIDGETPLUGIN_H

#include <QDesignerCustomWidgetInterface>

class SpreaSheetPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
SpreaSheetPlugin(QObject *parent = 0);

bool isContainer() const;
bool isInitialized() const;
QIcon icon() const;
QString codeTemplate() const;
QString domXml() const;
QString group() const;
QString includeFile() const;
QString name() const;
QString toolTip() const;
QString whatsThis() const;
QWidget *createWidget(QWidget *parent);
void initialize(QDesignerFormEditorInterface *core);

private:
bool initialized;
};

#endif

//customwidgetplugin.cpp
#include "spreadsheet.h"
#include "customwidgetplugin.h"

#include <QtPlugin>

SpreaSheetPlugin::SpreaSheetPlugin(QObject *parent)
: QObject(parent)
{
initialized = false;
}

void SpreaSheetPlugin::initialize(QDesignerFormEditorIn terface * /* core */)
{
if (initialized)
return;

initialized = true;
}

bool SpreaSheetPlugin::isInitialized() const
{
return initialized;
}

QWidget *SpreaSheetPlugin::createWidget(QWidget *parent)
{
return (new SpreadSheet(2,3, parent));
}

QString SpreaSheetPlugin::name() const
{
return "SpreadSheet";
}

QString SpreaSheetPlugin::group() const
{
return "Display Widgets [Examples]";
}

QIcon SpreaSheetPlugin::icon() const
{
return QIcon();
}

QString SpreaSheetPlugin::toolTip() const
{
return "";
}

QString SpreaSheetPlugin::whatsThis() const
{
return "";
}

bool SpreaSheetPlugin::isContainer() const
{
return false;
}

QString SpreaSheetPlugin::domXml() const
{
return "<widget class=\"SpreadSheet\" name=\"spreadSheet\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>300</width>\n"
" <height>300</height>\n"
" </rect>\n"
" </property>\n"
" <property name=\"toolTip\" >\n"
" <string> Spread Sheet</string>\n"
" </property>\n"
" <property name=\"whatsThis\" >\n"
" <string> Spread sheet: Items are displayed "
" in table format.</string>\n"
" </property>\n"
"</widget>\n";
}

QString SpreaSheetPlugin::includeFile() const
{
return "spreadsheet.h";
}

QString SpreaSheetPlugin::codeTemplate() const
{
return "";
}

Q_EXPORT_PLUGIN2(customwidgetplugin, SpreaSheetPlugin)

jacek
15th March 2006, 12:35
It probably creates the right object, but your custom widget is wrong.

It should be:
class SpreadSheet : public QTableWidget
{
Q_OBJECT
public :
SpreadSheet( QWidget* a_parent = 0 );
SpreadSheet( int a_row = 1, int a_column = 4, QWidget *a_parent = 0 );
void CreatePopupMenu();

private slots:
void ChooseColor();
void ExecPopupMenu( int, int );

private:
unsigned row;
unsigned column;
Q3PopupMenu *menu;
QAction *colorBackAct;
QAction *colorTextAct;
QColor color;
};
PS. Next time, please, use [ code ] tags to make your code readable.

jnana
15th March 2006, 12:49
Thanks,

I was thinking Q_OBJECT macro is used for Signals and Slots.

I have one more doubt about width and height of each cell in QTableWidget. Except using methods like setColumnWidth(), setRowHeight(), is there any function which will create all cell of same width and height.

Thanks
Jnana

jacek
15th March 2006, 12:55
I was thinking Q_OBJECT macro is used for Signals and Slots.
It is, but the problem was that you didn't declare ChooseColor() and ExecPopupMenu( int, int ) as slots.


Except using methods like setColumnWidth(), setRowHeight(), is there any function which will create all cell of same width and height.
You could try changing the size hint for QTableWidgetItems.