Results 1 to 4 of 4

Thread: Problem with custom widget

  1. #1
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with custom widget

    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)
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with custom widget

    It probably creates the right object, but your custom widget is wrong.

    It should be:
    Qt Code:
    1. class SpreadSheet : public QTableWidget
    2. {
    3. Q_OBJECT
    4. public :
    5. SpreadSheet( QWidget* a_parent = 0 );
    6. SpreadSheet( int a_row = 1, int a_column = 4, QWidget *a_parent = 0 );
    7. void CreatePopupMenu();
    8.  
    9. private slots:
    10. void ChooseColor();
    11. void ExecPopupMenu( int, int );
    12.  
    13. private:
    14. unsigned row;
    15. unsigned column;
    16. Q3PopupMenu *menu;
    17. QAction *colorBackAct;
    18. QAction *colorTextAct;
    19. QColor color;
    20. };
    To copy to clipboard, switch view to plain text mode 
    PS. Next time, please, use [ code ] tags to make your code readable.

  3. #3
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with custom widget

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with custom widget

    Quote Originally Posted by jnana
    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.

    Quote Originally Posted by jnana
    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.

Similar Threads

  1. Widget background problem.
    By zgulser in forum Qt Tools
    Replies: 2
    Last Post: 6th January 2009, 07:34
  2. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 16:38
  3. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 14:12
  4. Custom tab widget question
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 12:17
  5. Replies: 4
    Last Post: 2nd March 2006, 00:11

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.