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. Qt emit signal problem
Qt 6.11 is out! See what's new in the release blog

Qt emit signal problem

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.8k Views 2 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
    katana
    wrote on last edited by SGaist
    #1

    Hi everyone here
    i'm new in here and Qt.(having a lot problems)
    what im doing is to draw a point (later a line a circle maybe) in a graphicsview mit mouse click
    what ive done is like that:

    mainwindow.h

    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void paintEvent(QPaintEvent *ev);
    
    private:
        Ui::MainWindow *ui;
        myview view;
    };
    

    myview.h

    #include <QGraphicsView>
    #include <QMouseEvent>
    #include <QEvent>
    
    class myview : public QGraphicsView
    {
        //Q_OBJECT
    public:
        myview(QWidget *parent = 0);
    
        int x,y;
    
    protected:
        void mousePressEvent(QMouseEvent *ev);
    
    signals:
        void Mousepressed();
    };
    

    myview.cpp

    #include <QMouseEvent>
    
    myview::myview(QWidget *parent) : QGraphicsView(parent)
    {
    }
    void myview::mousePressEvent(QMouseEvent *ev)
    {
        x = ev->pos().x();
        y = ev->pos().y();
        emit Mousepressed();
    }
    

    mainwindow.cpp

    #include <QPainter>
    #include <myview.h>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QObject::connect(ui->graphicsView,SIGNAL(Mousepressed()),this,SLOT(paintEvent(QPaintEvent *ev)));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::paintEvent(QPaintEvent *ev)
    {
        QPainter p(this);
        QPen pen(Qt::black);
        pen.setWidth(6);
        p.setPen(pen);
        p.drawPoint(view.x,view.y);
    }
    

    when i bild the project i get this error:
    undefined reference to myview::Mousepressed()

    Thanks in advance

    [edit: corrected coding tags: use ```before and after SGaist]

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

      Hi and welcome to devnet,

      You commented out the Q_OBJECT macro from your myview class so moc doesn't run thus your signals are not implemented like they should be.

      On a side note, your connection statement is invalid for two reasons:

      • list item paintEvent is not a slot
      • you can't connect a parameterless signal to a slot with parameters.

      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
        katana
        wrote on last edited by
        #3

        @SGaist Thank U so much. The Signal emit problem is solved perfect. I wanna ask how can I call the paintevent with a mouse click?

        thanks in advance

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

          You can't. paintEvent is called when needed, you can ask for a repaint but it doesn't mean it will be done immediately

          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