0

I'm having issues figuring out why I can't return my function. Any ideas?

The errors I get are; expected primary-expression before '*' token and before ')' token.

(unsure if this is a C++ or Qt error, I'm no expert in both)

int MainWindow::createPacket(const QString &source)

    {
        QGraphicsView *editor = new QGraphicsView;
        int tabIndex = packet->addTab(editor, source); 
        packet->setCurrentIndex(tabIndex);

        return paintEvent(QPaintEvent*); <<<<<<<<< The line the error appears on.
    }

    void MainWindow::paintEvent(QPaintEvent*)
    {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setPen(Qt::black);

        QRect rect = QRect(10, 20, 70, 40);
        painter.drawText(rect, Qt::AlignCenter,
                         "Source");
        painter.drawRect(rect);
    }
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ash
  • 125
  • 1
  • 1
  • 9

2 Answers2

2

QPaintEvent* is a type. When you call a function, you don't pass objects as parameters, not type.

Also, since the parameter to paintEvent isn't named, nor used, why is it there at all? Why not simply use:

void MainWindow::paintEvent()
{
   //...
}

The code is invalid, and I can't imagine how it could work, or what you're expecting to happen. This is basic stuff, before you start off with Qt I suggest you first learn C++ (this isn't meant to be condescending, just a piece of advice).

A valid call would be, for example:

QPaintEvent* qpaintEventPointer = NULL;
paintEvent(qpaintEventPointer);

but this is invalid because paintEvent returns void, whereas the calling context returns int.

To return a function itself, you need to return a function pointer, not an int, as you do, and you don't need the full signature, just a return paintEvent...

Bottom line.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • @LightnessRacesinOrbit it's okay since it's not used inside the function. – Luchian Grigore Dec 30 '12 at 20:21
  • The function you're sending it to doesn't accept it by reference, so what's the point? Also I think technically this is UB, since copying it is using it. – Lightness Races in Orbit Dec 30 '12 at 20:27
  • Thanks for the advice, the reason I had the parameter to paintEvent was due to previous errors saying QPainter not active when the parameter wasn't included. (Fixed for a while). As I say i'm no expert in C++, been learning it for 1 year. i'm just trying to combine it with some graphics. Thanks though :) – Ash Dec 30 '12 at 20:37
  • @LightnessRacesinOrbit you sure? I don't think it's UB just copying it. It's ok as long as it's not dereferenced. – Luchian Grigore Dec 30 '12 at 21:32
  • 1
    It's undefined in C++98, C++03 and C++11, but that's a defect. It's implementation-defined in the latest WP, see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1438, most implementations are likely to document that it's OK to copy an invalid pointer as long as it's not dereferenced. – Jonathan Wakely Dec 30 '12 at 22:27
  • @Luchian: What Jonathan said. _For now_, technically as I indicated, it's UB. – Lightness Races in Orbit Dec 30 '12 at 23:36
  • @LuchianGrigore: Every day something new, etc. – Lightness Races in Orbit Dec 30 '12 at 23:40
  • http://stackoverflow.com/questions/7346634/dereferencing-an-invalid-pointer-then-taking-the-address-of-the-result#comment8862422_7346634 - even having an invalid pointer value, if you "use" it at all, is invalid. Just because addresses are numbers in all known implementations doesn't mean they can't be something else, and having an address to nowhere defined cannot itself be defined. I would support the proposal in #1438 to make this implementation-defined instead. – Lightness Races in Orbit Dec 30 '12 at 23:43
1

Even if you had called MainWindow::paintEvent() correctly, this function returns nothing, and is supposed to be called automatically when the system needs to draw your widget (See the documentation)

I'm not sure of what you were trying to do, but to trigger a repaint you want to call repaint(), not paintEvent().

Khoyo
  • 1,253
  • 11
  • 20
  • Maybe I'm using the wrong widget for what I want to do. I'll have a rethink. – Ash Dec 30 '12 at 21:03
  • note: `repaint()` is synchronous. If you want to schedule paint event use `update()` instaed. – Kamil Klimek Dec 31 '12 at 09:19
  • I want to move away from PaintEvent *. I have no use for it (as pointed out ^^^). I just want to draw a rectangle using QPainter. Thanks – Ash Dec 31 '12 at 11:15