Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Custom QPainter widget not drawing
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Custom QPainter widget not drawing

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kloveridge
    wrote on last edited by
    #1

    I am trying to make a simple drawing widget that draws a line. I've been at this for a hour or so and can't understand what i'm dong wrong. Here's the .h/.cpp

    Header
    @#ifndef NILEEXPANDER_H
    #define NILEEXPANDER_H

    #include <QWidget>

    class NileExpander : public QWidget
    {
    Q_OBJECT
    public:
    NileExpander(QWidget *parent = 0);

    protected:
    void paintEvent(QPaintEvent *);

    signals:

    public slots:

    };

    #endif // NILEEXPANDER_H@

    CPP
    @#include "nileexpander.h"
    #include <QPainter>
    #include <QDebug>

    NileExpander::NileExpander(QWidget *parent) :
    QWidget(parent)
    {
    qDebug() << "Init Nile Expander.";
    resize(300,25);
    }

    void NileExpander::paintEvent(QPaintEvent *)
    {
    qDebug() << "Paint Nile Expander.";
    QPainter painter(this);
    painter.drawLine(0,0, 300,25);
    }
    @

    I have a have a QVBoxLayout I am trying to parent this to. Here's how I've implemented this custom widget:

    @ NileExpander* expander = new NileExpander(this);
    ui->NodeView->addWidget(expander);
    ui->NodeView->addWidget(m_nodeCommon);
    ui->NodeView->addWidget(m_nodeDrawable);

    ui->NodeView->addSpacerItem(spacer);@

    I've seen nothing coming through. The painEvent() function isn't getting called. The constructor is getting called, however.

    Any suggestions?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kloveridge
      wrote on last edited by
      #2

      I figured it out. I assumed the resize(300,25) would automatically set the size of the widget. Turns out I needed this to get it to work:

      @ NileExpander* expander2 = new NileExpander();
      expander2->setFixedWidth(300);
      expander2->setFixedHeight(25);
      ui->NodeView->addWidget(expander2);@

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Rather than hardcode sizes like that, why not just use width() and height() in your paint event ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kloveridge
          wrote on last edited by
          #4

          Yea, thats good advice. Now that I've got something coming through, I'll get more fancy. ;)

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            If this solves your problem, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved