Ole-M
Ole-M

Reputation: 821

Create listening QLabel

I am used to Java and new to QT/C++, and I have some problems creating a QLabel which changes text whenever a person is selecting a QListWidgetItem.

In my ui_GraphicsView.h I have setupUI() which makes the objects. I try to make a layout its parent.

label = new QLabel(verticalLayout);
   label->setObjectName(QString::fromUtf8("label"));
   verticalLayout->addWidget(label);

In my .cpp-file I make use of connect in the constructor:

connect(list_widget, SIGNAL(itemSelectionChanged(), this, SLOT(updataDetails())));

updateDetails() is executed in my selectionChanged() method where it passes a string.

void GraphicsView::updateDetails(QString details){
    label->setText(details);
}

This is resulting in the following error:

error: no matching function for call to ‘QLabel::QLabel(QVBoxLayout*&)
note: candidates are: QLabel::QLabel(const QLabel&)
note: QLabel::QLabel(const QString&, QWidget*, Qt::WindowFlags)
note: QLabel::QLabel(QWidget*, Qt::WindowFlags)

Everything worked out well before I made the adjustments described above. Any idea what is causing this error?

Upvotes: 0

Views: 465

Answers (1)

KAction
KAction

Reputation: 2017

Read the error. QLabel::QLabel is constructor. Constructor expects pointer to widget, not to layout. In fact, you want to do following:

label = new QLabel(parent_widget);
label->setLayout(layout)

EDIT: Strange, nobody noticed my error. Setting layout on label is strange. Better is

label=new QLabel();
layout->addWidget(label).

Label will get owner of layout as parent.

Upvotes: 2

Related Questions