Results 1 to 7 of 7

Thread: signal-slot issue, how to create a signal when a Qlist is updated

  1. #1
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default signal-slot issue, how to create a signal when a Qlist is updated

    Hi,

    I am re-posting my question. This time I hope I am more clear.

    In my GraphicView I have an event:
    Qt Code:
    1. void MyGraphicsView::mousePressEvent(QMouseEvent * e)
    2. {
    3. if (e->buttons().testFlag(Qt::MidButton))
    4. {
    5. double rad = 2;
    6. QPointF pt = mapToScene(e->pos());
    7. QPen myPen = QPen(Qt::red);
    8. this->Scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0,myPen, QBrush(Qt::SolidPattern));
    9. this->list.append(pt);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    I want to create a connection between my main window's QLabel and MyGraphicsView::list.append(pt); that every time a new value is added to the list, pt will be printed on QLabel of the main window.
    I can create a slot in my main window that will print a string on QLabel but I don't know what will be my signal and how the connection command will be look like.
    Basically, I don't know how to create a signal when a list is updated.

    I hope I can get help on this matter.

    Thank,
    Last edited by anda_skoa; 3rd March 2014 at 22:38. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: signal-slot issue, how to create a signal when a Qlist is updated

    Just add a signal to your graphics view class

    Qt Code:
    1. clas MyGraphicsView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5. signals:
    6. void pointAdded(const QPointF &point);
    7. };
    To copy to clipboard, switch view to plain text mode 
    And emit it when you add a new point
    Qt Code:
    1. emit pointAdded(pt);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    -

  3. #3
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: signal-slot issue, how to create a signal when a Qlist is updated

    Thanks. I added the signal to MyGraphicView class and emitted it as you suggested. My connection statement looks like:
    QObject::connect(ui->widget1, SIGNAL(pointAdded(const QPointF &point)), ui->label1, SLOT(printPointCoordinates(QPointF pt)));
    The project is built with no errors but when I run it I am getting this message:
    QObject::connect: No such signal MyGraphicsView:ointAdded(const QPointF &point) in ..\graphicsviewer_app\mainwindow.cpp:22
    QObject::connect: (sender name: 'widget1')
    QObject::connect: (receiver name: 'label1')


    My slot in the main window is:
    void MainWindow:rintPointCoordinates(QPointF pt)
    {
    qDebug()<<"Into printPointCoordinates";
    QString str = "( " + QString::number((int)pt.x())+ ", " + QString::number((int)pt.y())+ " )";
    ui->label1->setText(str);
    }

    What do you think is the problem?

    Thanks

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,554
    Thanks
    13
    Thanked 1,581 Times in 1,510 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signal-slot issue, how to create a signal when a Qlist is updated

    Firstly, the parameter names do not form part of the signal or slot signature. This lead to the error message you got. You correct that like:
    Qt Code:
    1. QObject::connect(ui->widget1, SIGNAL(pointAdded(const QPointF &)), ui->label1, SLOT(printPointCoordinates(QPointF)));
    To copy to clipboard, switch view to plain text mode 

    Secondly, connecting the signal to a QLabel object that does not have a slot named printPointCoordinates() will not work. You have declared and defined the slot in the MainWindow class so the target of the connect should be an instance of that class.

  5. #5
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: signal-slot issue, how to create a signal when a Qlist is updated

    Thank you for your quick respond. I changed the connect statement as you suggested and I also changed the receiver to be the main window where the slot is defined. Now everything is working. I still have an issue with the printout. The print statement is: ui->label1->setText(str); so every time I select a new point, it overwrites the printout of the previous point. Is there a way to print a new string in a new line?
    Thanks.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,554
    Thanks
    13
    Thanked 1,581 Times in 1,510 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signal-slot issue, how to create a signal when a Qlist is updated

    A QLabel is for short messages, usually as single line, and you have coded it to replace the entire text so that is what happens. If you want longer tracts of text then appending to a QPlainTextEdit or QTextEdit would be a better choice. A QListWidget may also be a choice.

  7. #7
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: signal-slot issue, how to create a signal when a Qlist is updated

    Thank you. QTextEdit and ui->textEdit1->append(str) did the work.

Similar Threads

  1. a signal-slot issue
    By ceraolo in forum Newbie
    Replies: 2
    Last Post: 28th April 2012, 19:30
  2. Signal from QList Object to Slot from parent
    By Rocken in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2012, 15:34
  3. Signal / Slot issue
    By sicker in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2011, 22:53
  4. Signal and Slot Issue
    By dlrlsqa1 in forum Newbie
    Replies: 5
    Last Post: 23rd March 2009, 13:44
  5. queued signal/slot connection with QList<int>
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2006, 15:32

Tags for this Thread

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.