Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Ui passing widget to class causes segmentation fault
Forum Updated to NodeBB v4.3 + New Features

Ui passing widget to class causes segmentation fault

Scheduled Pinned Locked Moved C++ Gurus
8 Posts 5 Posters 6.8k 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.
  • R Offline
    R Offline
    ripspinner
    wrote on last edited by
    #1

    I'm trying to add an QGLwidget to a widget container in a UI via the Ui's cpp file:
    @AffectCube::AffectCube(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AffectCube)
    {
    if (!QGLFormat::hasOpenGL()) {
    cerr << "This system has no OpenGL support" << endl;
    close();
    }
    Tetrahedron tetrahedron(ui->widget);
    tetrahedron.setWindowTitle(QObject::tr("Tetrahedron"));
    tetrahedron.resize(300, 300);
    tetrahedron.show();

    ui->setupUi(this);
    

    }@

    A segmentation fault occures in the construction of the tetrahedron:
    @
    Tetrahedron::Tetrahedron(QWidget *parent)
    : QGLWidget(parent)
    {
    setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
    rotationX = -21.0;
    rotationY = -57.0;
    rotationZ = 0.0;
    faceColors[0] = Qt::red;
    faceColors[1] = Qt::green;
    faceColors[2] = Qt::blue;
    faceColors[3] = Qt::yellow;

    }@

    I'm at a loss on why this is or how to fix it... help!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      Are you sure ui->widget on line 9 is something valid, before you call setupUi on line 16?

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ripspinner
        wrote on last edited by
        #3

        I tried doing the same thing after setup with the same result.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4
          1. Where's the crash exactly?
          2. Why are you allocating tetrahedron on the stack?

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            It should be
            @
            AffectCube::AffectCube(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::AffectCube)
            {
            if (!QGLFormat::hasOpenGL()) {
            cerr << "This system has no OpenGL support" << endl;
            close();
            }
            ui->setupUi(this);
            Tetrahedron tetrahedron(ui->widget);
            tetrahedron.setWindowTitle(QObject::tr("Tetrahedron"));
            tetrahedron.resize(300, 300);
            tetrahedron.show();
            }@

            setupUI is the method, that creates all widgets and layouts them. Using the pointers before that call is using uninitialized memory. So take care, everything can happen then... :-)

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              ripspinner
              wrote on last edited by
              #6

              Ok fixed the code and allocated tetrahedron on the heap, that was my intention.

              Seems to work now thanks!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                I think allocate Tetrahedron on the stack is wrong.

                When the AffectCube constructor returns, the Tethraedron instance is destroyed

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  [quote author="mcosta" date="1296741507"]I think allocate Tetrahedron on the stack is wrong.

                  When the AffectCube constructor returns, the Tethraedron instance is destroyed[/quote]

                  ripspinner wrote that already in his last comment and it works.

                  Please read a thread until its end and only post comments if you have something new to add. Thanks.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  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