Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPushButton initialization
Forum Updated to NodeBB v4.3 + New Features

QPushButton initialization

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HYSuperman
    wrote on last edited by
    #1

    When I initialize a QPushButton Object, I can do it this way:
    @QPushButton p1;@

    I can also do it this way:
    @QPushButton *p2 = new QPushButton;@

    The first way initialize an object while the later way initialize a pointer.
    But when I add either of them to the widget and show this widget, the first way doesn't show.
    I wonder why? As far as I know, the QPushButton class has a constructor:QPushtButton(QWidget *parent = 0);, so I don't think the construction process will go wrong...

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      Quentique
      wrote on last edited by
      #2

      For me, it works. My code :
      @ QWidget test;
      QPushButton p1(&test);
      p1.setGeometry(1, 1, 50, 40);
      p1.setText("Hello");
      QPushButton *p2 = new QPushButton(&test);
      p2->setGeometry(30, 20, 50, 40);
      p2->setText("HG");
      p1.show();
      p2->show();
      test.show();@

      I think that you don't set the position of yours buttons.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If you do it e.g. in the constructor, p1 will be destroyed at the end of the constructor since it goes out of scope. That's why you don't see it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved