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. I create a QPushButton instance before ui->setupUi(this), And this button wont clicked
Forum Updated to NodeBB v4.3 + New Features

I create a QPushButton instance before ui->setupUi(this), And this button wont clicked

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 280 Views 2 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.
  • N Offline
    N Offline
    NULL_NULL
    wrote on 14 Jan 2024, 08:11 last edited by
    #1

    as title says, in Qt6.6.1, I add a private (QPushButton *) member in MainWindow and created in the construct list.
    code is shown below:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        , m_button(new QPushButton("KSKSKSKSK", this))
    {
        ui->setupUi(this);
        qDebug() << "object: "<< this <<"is created!";
        // LetsBegin();
        m_button->setGeometry(10,20,300,100);
        QObject::connect(m_button, &QPushButton::clicked,qApp, &QApplication::quit);
    }
    

    however when i run this, this button cant be clicked, and no apparence changing even when cursor move through.

    by search some other website, i find that ui->setupUi(this) sort of "cover" those QWidget created before this line

    I wonder if there is any way to "re_activate" this m_button?
    Or i have to create this instance after ui->setupUi(this) anyway?

    J 1 Reply Last reply 15 Jan 2024, 06:46
    0
    • N Offline
      N Offline
      NULL_NULL
      wrote on 14 Jan 2024, 08:24 last edited by
      #2

      I changed ths init method by using m_button( (ui->setupUi(this), new QPushButton("KSKSKSKSK", this))), this seems worked(mainly becase i declear m_button after ui)
      but still not a solution since this way only allow me to use construct list rather than create instance before ui->setupUi(this)

      A 1 Reply Last reply 14 Jan 2024, 09:07
      0
      • N NULL_NULL
        14 Jan 2024, 08:24

        I changed ths init method by using m_button( (ui->setupUi(this), new QPushButton("KSKSKSKSK", this))), this seems worked(mainly becase i declear m_button after ui)
        but still not a solution since this way only allow me to use construct list rather than create instance before ui->setupUi(this)

        A Offline
        A Offline
        Axel Spoerl
        Moderators
        wrote on 14 Jan 2024, 09:07 last edited by
        #3

        @NULL_NULL
        m_buttonis directly parented to MainWindow, which at this point doesn't have a central widget. That's added (amongst other things) in setupUi(). Hard to say where and why exactly things go wrong, but it's somewhere there.

        It'll be better to construct the button without a parent in the first place:

        , m_button(new QPushButton("KSKSKSKSK"))
        

        You can add it after calling setupUi(), e.g. with

        m_button->setParent(centralWidget());
        

        or by adding it to a layout with

        ui->anyLayout->addWidget(m_button);
        

        Software Engineer
        The Qt Company, Oslo

        1 Reply Last reply
        3
        • S SGaist moved this topic from Qt in Education on 14 Jan 2024, 09:28
        • N NULL_NULL
          14 Jan 2024, 08:11

          as title says, in Qt6.6.1, I add a private (QPushButton *) member in MainWindow and created in the construct list.
          code is shown below:

          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
              , m_button(new QPushButton("KSKSKSKSK", this))
          {
              ui->setupUi(this);
              qDebug() << "object: "<< this <<"is created!";
              // LetsBegin();
              m_button->setGeometry(10,20,300,100);
              QObject::connect(m_button, &QPushButton::clicked,qApp, &QApplication::quit);
          }
          

          however when i run this, this button cant be clicked, and no apparence changing even when cursor move through.

          by search some other website, i find that ui->setupUi(this) sort of "cover" those QWidget created before this line

          I wonder if there is any way to "re_activate" this m_button?
          Or i have to create this instance after ui->setupUi(this) anyway?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 15 Jan 2024, 06:46 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0

          4/4

          15 Jan 2024, 06:46

          • Login

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