Results 1 to 5 of 5

Thread: Dealing with pointer in Qt

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Dealing with pointer in Qt

    Hey everyone!

    I have a very basic question.

    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
    Qt Code:
    1. // constructor of MyDialog class ///////////////////////////////////
    2.  
    3. MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
    4. {
    5. QLabel *newLbl = new QLabel("New");
    6.  
    7. QVBoxLayout *newLay = new QVBoxLayout(this);
    8. newLay.add(newLbl);
    9. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Dealing with pointer in Qt

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dealing with pointer in Qt

    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.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Dealing with pointer in Qt

    However, when I used such a selfmade widget within a QTabWidget. All the labels are gone.
    What do you mean?

  5. #5
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dealing with pointer in Qt

    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.

Similar Threads

  1. Problem dealing with resizeEvent
    By Momergil in forum Qt Programming
    Replies: 8
    Last Post: 17th January 2013, 19:43
  2. Dealing with Layout expanding size
    By edxxgardo in forum Qt Programming
    Replies: 2
    Last Post: 16th August 2011, 10:20
  3. Problem dealing with QDialog
    By baluk in forum Newbie
    Replies: 2
    Last Post: 16th January 2011, 15:01
  4. Replies: 1
    Last Post: 4th December 2010, 17:20
  5. dealing individual objects
    By kingslee in forum General Programming
    Replies: 9
    Last Post: 15th November 2006, 01:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.