[solved]How to add a widget to window, when it is generated by method
-
wrote on 4 Jun 2015, 04:55 last edited by Lord of Noob 6 May 2015, 06:31
Hi there,
I want in my app to generate some default Wifdget arrangements by a method.
For example://adds a label and a QTextEdit field to my window. createItem(Name, InputType::Text);
My problem is that this is not working. If I try to return the objects from the Method, I run into an error which I do not understand as noob.
/usr/include/qt/QtWidgets/qlabel.h:142: Fehler: 'QLabel::QLabel(const QLabel&)' is private Q_DISABLE_COPY(QLabel)
If I define the naked pointers in the class as private variables the application Crashes.
Mainwindow.hclass MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent, std::string appname); ~MainWindow(); void createCharItem(QString name, QString txtContent); private: //variables QVBoxLayout *section; //Stacks all items below each other QLabel *title; // one Item to add //functions void createTitle(QString name);
}
implementation of void createTitle(QString name);
void MainWindow::createTitle(QString name) { QLabel *title = new QLabel; title->setText(name); title->setLineWidth(40); int height = title->height(); title->resize(this->width(),height); section->addWidget(title); }
What do I miss?
Or how to do this? -
wrote on 4 Jun 2015, 05:39 last edited by
Hi and welcome to devnet,
can you show the implementation of
createCharItem()
?
QObject
(or inherited classes) instances cannot be created on the stack (you have to use pointers) -
Hi and welcome to devnet,
can you show the implementation of
createCharItem()
?
QObject
(or inherited classes) instances cannot be created on the stack (you have to use pointers)wrote on 4 Jun 2015, 15:41 last edited by Lord of Noob 6 Apr 2015, 16:20@mcosta
Thanks the Welcome,Ohh, I am sorry i was tired. The
createTitle();
is a reduced version of
createItem();
I do not have the createItem yet. It will look similar only it will consinst of a 1xQHBoxLayout,1xQLabel and 1xQTestEdit or 1xQSpinBox.
My Constructer of QMainWindow looks like:
MainWindow::MainWindow(QWidget *parent,std::string appname) :QMainWindow(parent) { QTabWidget *charsheet = new QTabWidget; this->setCentralWidget(charsheet); QWidget *titlepage = new QWidget(this); QWidget *combatpage = new QWidget; charsheet->addTab(titlepage,"Heerosheeet"); charsheet->addTab(combatpage,"combatsheet"); QVBoxLayout *section = new QVBoxLayout(titlepage); createTitle(QString::fromStdString(appname)); /* If I do it this way it works ... QLabel *title = new QLabel; title->setText(QString::fromStdString(appname)); title->setLineWidth(40); int height = title->height(); title->resize(this->width(),height); section->addWidget(title); */ }
Ohh I just have seen if I move the
section->addWidget(title);
to main, it works. But why I can not use the add Widget Method in my own method?
Edit:
Ok, fixed one Bug: QLabel was wrong. But the program is still crashing this way.
void MainWindow::createTitle(QString name) { title = new QLabel; title->setText(name); title->setLineWidth(40); int height = title->height(); title->resize(this->width(),height); section->addWidget(title); }
-
wrote on 4 Jun 2015, 16:36 last edited by
Okay, I now found 2 Exceptions:
QXcbWindow: Unhandled client message: "_E_COMP_FLUSH"
QXcbWindow: Unhandled client message: "_E_COMP_DUMP"I think I can catch them, but I do not know what to do with these Errors...
Google is not makeing any sense.
Marking the Handler and press F1 is not doing something :-DAny hints where to look?
-
wrote on 4 Jun 2015, 23:01 last edited by
My problem is that this is not working. If I try to return the objects from the Method, I run into an error which I do not understand as noob.
/usr/include/qt/QtWidgets/qlabel.h:142: Fehler: 'QLabel::QLabel(const QLabel&)' is private Q_DISABLE_COPY(QLabel)There is an attempt to copy QObject derived object, probably cause you return object not the pointer.
If You need to create object of QWidget (QObject) derived class in a function, function should look like:
QWidget* createWidget();
not a
QWidget createWidget (), cause in such case object will have to be copied and it is forbidden. -
wrote on 5 Jun 2015, 06:30 last edited by
Cool, this works.
I return now a Pointer, and add it in one line to the screen.I mark this solved. Even I still have no Idea why the one way does not work.
great thanks.
-
Hi,
Because QObject is not a copyable class. See here for more information
1/7