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:
Code:
{
if (e->buttons().testFlag(Qt::MidButton))
{
double rad = 2;
this
->Scene
->addEllipse
(pt.
x()-rad, pt.
y()-rad, rad
*2.0, rad
*2.0,myPen,
QBrush(Qt
::SolidPattern));
this->list.append(pt);
}
}
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,
Re: signal-slot issue, how to create a signal when a Qlist is updated
Just add a signal to your graphics view class
Code:
{
Q_OBJECT
signals:
void pointAdded
(const QPointF &point
);
};
And emit it when you add a new point
Code:
emit pointAdded(pt);
Cheers,
-
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::pointAdded(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::printPointCoordinates(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
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:
Code:
QObject::connect(ui
->widget1,
SIGNAL(pointAdded
(const QPointF &)), ui
->label1,
SLOT(printPointCoordinates
(QPointF)));
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.
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.
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.
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.