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. Qt progra crashing when using ui->
Forum Updated to NodeBB v4.3 + New Features

Qt progra crashing when using ui->

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 620 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.
  • E Offline
    E Offline
    emeriska
    wrote on last edited by
    #1

    Hi, so I'm setting up this program with Qt Designer, but for one part I need to be able to control (add and delete) button from the program so I use a QList and I code the widget myself without Qt Designer.

    The thing is when in my code I use ui-> to refer to a widget created with Qt Designer my program crash. Everytime. In every cases. I mean I tried doing basic setGeometry for some widget and as soon as I use ui->ExampleWidget->whateverFunction(); the program crashes on startup with the famous "Stopped responding error"

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    addEmploye();
    ui->setupUi(this);
    }

    void MainWindow::addEmp()
    {
    listEmp.append(new QPushButton("Text Here"));
    thislayout = new QGridLayout;
    thislayout->addWidget(listEmp.back(), 0, 0);

    ui->empWidget->setLayout(thislayout);        //runs fine if I remove this line
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    delete listEmp[0];
    }
    @

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtWidgets>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    void addEmp();
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    QList<QPushButton*> listEmp;
    QGridLayout *thislayout;
    

    };

    #endif // MAINWINDOW_H
    @

    Thanks a lot guys!

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

      Hi and welcome to devnet,

      @
      {
      addEmploye();
      ui->setupUi(this);
      }@

      The problem is that you are calling addEmploye first. Doing so, ui has not been initialized yet so none of its widgets exists. ui->setupUi(this) must be called first.

      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