PDA

View Full Version : Dealing with pointer in Qt



camelot
18th June 2013, 12:29
Hey everyone!

I have a very basic question.:o

Usually, I save the pointer to dynamically created objects as members of the class that created it.
However, in Qt there is this automatic dealing with pointers by parent objects.
Can I create a simple label dynamically within the constructor of a selfmade dialog, add it to a layout and this is it?
Some thing like


// constructor of MyDialog class ///////////////////////////////////

MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
{
QLabel *newLbl = new QLabel("New");

QVBoxLayout *newLay = new QVBoxLayout(this);
newLay.add(newLbl);
}


The QDialog is just an example. It could be also any ordinary class that inherits QWidget.

I was doing it like this for a while and it seemed to work out fine. However, when I used such a selfmade widget within a QTabWidget. All the labels are gone.

Any answer would be appreciated.

David

Santosh Reddy
18th June 2013, 13:02
As far as MyDialog example is concerned it is just fine and is usual way may would do. There might be some other problem with QTableWidget. BTW i think it should be newLay.addWidget() in the code.

camelot
18th June 2013, 13:46
Ok, thank you.
I just grew accustomed to this method but I always felt a little uneasy.

You are right about the newLay.addWidget(...).
I didn't copied it from Creator. I just typed it in from memory.

I never used a QTabWidget before. I used the selfmade object (with the labels that got lost) before on its own within a QScrollArea without any problems.
Now that I have to add more functionality to the program. I decided to put it in a QTabWidget and put this one into the scroll area and now the labels are gone.

ChrisW67
18th June 2013, 23:44
However, when I used such a selfmade widget within a QTabWidget. All the labels are gone.
What do you mean?

camelot
19th June 2013, 09:01
I have a custom widget with QLabels (defined as shown above) sitting in a QGridLayout. This custom widget was set in a QScrollArea, which was set as the central widget of a QMainWindow. Everything worked fine.
Now, I set my custom widget first in one tab of a QTabWidget, set the tab widget in the QScrollArea which is still the central widget of a QMainWindow. Now, the text of all labels in the custom widget are not visible anymore. There are other elements in the same layout of the custom widget as QSpinBoxes, QPushButtons which are all rendered perfectly.
I just wonder what had happened.