Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [solved] Qt4 - Program written in QtCreator segfaults

    General and Desktop
    2
    5
    2456
    Loading More Posts
    • 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
      nexuiz last edited by

      The following code is suppose to change a property of a progress bar when a button is clicked.
      I tried to do the same in Qt3Designer and everything worked fine.
      What happens is as soon as the button is clicked, the program crashes with the following message:

      Starting /home/user/qt4/test1/test1...
      The program has unexpectedly finished.
      /home/user/qt4/test1/test1 exited with code 0

      Here's mainwindow.cpp
      @
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "kled.h"
      #include <QLCDNumber>
      #include <QProgressBar>

      QProgressBar *qprogressbar;
      QLCDNumber *lcdnumber;
      KLed *kled;

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {

      ui->setupUi(this);
      

      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      void MainWindow::changeEvent(QEvent *e)
      {

      QMainWindow::changeEvent(e);
      switch (e->type()) {
      case QEvent::LanguageChange:
          ui->retranslateUi(this);
          break;
      default:
          break;
      }
      

      }

      void MainWindow::on_pushButton_clicked()
      {
      qprogressbar->setValue(30);
      }

      @

      Thank you.

      1 Reply Last reply Reply Quote 0
      • M
        mlong last edited by

        Where are you instantiating qprogressbar?

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply Reply Quote 0
        • N
          nexuiz last edited by

          Thanks for your quick reply.
          Disclaimer: I am not a C++ programmer. I just create frontends for my C based console programs that drive my instruments.

          Back to my post now, I thought that QtCreator did it automatically (pretty much like in Qt3-Designer).
          I mean since all widgets already were placed on the form, then they should already have been instantiated, right?

          1 Reply Last reply Reply Quote 0
          • M
            mlong last edited by

            Based on your comments, I'm assuming you have a widget in the Ui named qprogressbar?

            If so, you need to get rid of line 7 in your code above ( (It doesn't refer to the progressbar in your ui. It's just an unused global pointer) , and replace line 42 with
            @
            ui->qprogressbar->setValue(30)
            @
            as anything that is created in the Ui file will be available through that pointer. There's no need to redeclare pointers for the Ui widgets in your code.

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply Reply Quote 0
            • N
              nexuiz last edited by

              That worked. Next stop: get the fundamentals of C++.

              Thank you very much.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post