0

I have one abstract class, TableWidget, which inherits from QWidget. It implements paintEvent.

class TableWidget : public QWidget
{
    Q_OBJECT

private:
    int width;
    int height;

public:
    explicit TableWidget(QWidget *parent = 0);

    virtual int getWidthInCells() = 0;
    virtual int getHeightInCells() = 0;

    virtual QString getCellText(byte x, byte y) = 0;
    virtual QString getColumnLabel(byte a) = 0;
    virtual QString getRowLabel(byte a) = 0;
    byte getCellHighlight(byte x, byte y);
    void setCellHighlight(byte x, byte y, bool highlighted);

    void setHeight(int h);
    void setWidth(int w);

protected:
    void paintEvent(QPaintEvent *e);

signals:

public slots:

};

I tried testing TableWidget out with a dummy class with next to no functionality.

class DummyTable : public TableWidget {

public:
    explicit DummyTable(QWidget *parent = 0) {}

    int getWidthInCells() {return 2;}
    int getHeightInCells() {return 2;}

    QString getCellText(byte x, byte y) {return "0";}
    QString getColumnLabel(byte a) {return QString(a);}
    QString getRowLabel(byte a) {return QString(a);}

};

The problem is that paintEvent (implemented in TableWidget.cpp), is never called. The first mistake I noticed was the absence of the Q_OBJECT macro in DummyTable. I added it in and tested it, but this resulted in a failure to terminate correctly. I had to completely exit out of QT to kill the process. I looked into QPushButton and it seemed to do things the same as me, except for the inclusion of Q_OBJECT.

So, how can I get paintEvent to be called properly?

3 Answers 3

1

How have you added your DummyTable instance to the parent window?

You could try calling on the parent window the debug method dumpObjectTree ( http://qt-project.org/doc/qt-4.8/qobject.html#dumpObjectTree ) to check that your instance is correctly parented.

Also you could try implementing showEvent(...) and instrumenting that to check the widget is actually being displayed.

Have you looked at QTableWidget? Does it not do what you need? http://qt-project.org/doc/qt-4.8/qtablewidget.html

Regarding the QObject macro you only need that in classes where you're creating signal and slot connections, or using some of the other meta-programming features of Qt (such as properties). Its not likely to be the root cause of your issues.

Sign up to request clarification or add additional context in comments.

Comments

0

You may need to give your widget a proper width and height. If it has no size, paintEvent will not be called.

1 Comment

I read about that as a possibility, but that's not it; the .ui file is giving it a width and height. I also tried to do it manually, but it didn't work.
0

It turned out to be a pretty stupid mistake. I wasn't calling the parent class's constructor.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.