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

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 275 Views
  • 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 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?

    jsulmJ 1 Reply Last reply
    0
    • N Offline
      N Offline
      NULL_NULL
      wrote on 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)

      Axel SpoerlA 1 Reply Last reply
      0
      • N NULL_NULL

        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)

        Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on 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
        • SGaistS SGaist moved this topic from Qt in Education on
        • N NULL_NULL

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4
          This post is deleted!
          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