PDA

View Full Version : How to return QtListWidget from function



bandito
11th July 2016, 02:57
I am looking to create a QListWidget in a function and then return it. I get this error: main.cpp:112: error: could not convert 'lw' from 'QListWidget*' to 'QListWidget' return lw;
^

my function:

header:


private slots:
QListWidget create();



cpp


QListWidget Main::create(){

QListWidget* lw = new QListWidget;

lw->addItem(new QListWidgetItem("one"));
lw->addItem(new QListWidgetItem("two"));
return lw;
}

Lesiok
11th July 2016, 07:40
C++ ABC. lw is declared as a pointer.

d_stranz
11th July 2016, 17:35
And even if the syntax was fixed, it is impossible to return a QListWidget instance created on the stack since the copy constructor and assignment operator are disabled for QObject-based classes. You can only return a pointer to a QObject-based instance created on the heap.