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. [solved] Class object in mainwindow.h

[solved] Class object in mainwindow.h

Scheduled Pinned Locked Moved General and Desktop
13 Posts 2 Posters 3.0k 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.
  • S Offline
    S Offline
    sunil.nair
    wrote on last edited by
    #1

    Hello Guys, i hope you remember the cubeview class from qt3d. i want to use it from my mainwindow.

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "cubeview.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int getid();

    public slots:
    void pattern1();

    private:
    CubeView *view;
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    and mainwindow .cpp file
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPushButton>
    #include <iostream>
    #include "cubeview.h"

    int pattern_id;
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QPushButton *button= new QPushButton("Pattern 1");
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(pattern1()));
    button->show();
    std::cout<<"reached mainwindow constructor"<<std::endl;
    //view= new CubeView;

    }
    void MainWindow::pattern1()
    {
    pattern_id=1;
    view->begin(1);
    view->resize(800, 600);
    view->show();
    std::cout<<pattern_id<<std::endl;
    }
    int MainWindow::getid()
    {
    return pattern_id;
    }
    MainWindow::~MainWindow()
    {
    delete ui;
    delete view;
    }
    @
    I get a runtime error.
    I hope you get what i am trying to do. Whenever i click on the push button, the cubeview view window should show up. What is the mistake i am making?
    Where should I define the cubeView class object so that I can use it later.
    Can I initialize it as CubeView *view= new CubeView; in a header file.

    I tried to write it in the constructor of mainwindow.cpp
    but i get a runtime error.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Your view pointer is never initialized (with new keyword). Change it to use stack, or initialize the pointer.

      Also, please specify what runtime error are you getting.

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sunil.nair
        wrote on last edited by
        #3

        I try to initialize it in the constructor: as view=new Cubeview;
        but that gives me a runtime error:
        ASSERT failure in QGLPainter: "begin() has not been called or it failed", file painting\qglpainter.cpp, line 1665
        Invalid parameter passed to C runtime function.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          It is possible that this class is not intended to be run standalone. Please try debugging your application to see the exact stack trace and the place where it bails out.

          (Z(:^

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sunil.nair
            wrote on last edited by
            #5

            Sierdzio: please see the folllowing class, the cubeview object is displayed when showed inside a constructor. But, it is not displayed when written in a public slot: what could be the error.

            @#include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QPushButton>
            #include <iostream>
            #include "cubeview.h"

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

            std::cout<<"reached mainwindow constructor"<<std::endl;
            view=new CubeView;
            view->begin(1);
            view->resize(800, 600);
            view->show();
            if(view!=NULL)
            {
                QPushButton *button= new QPushButton("Pattern 1");
                QObject::connect(button, SIGNAL(clicked()), this,  SLOT(pattern1()));
                button->show();
            }
            else if(view==NULL)
            {
                 std::cout<<"view is not initialized"<<std::endl;
            }
            

            }
            void MainWindow::pattern1()
            {
            pattern_id=2;
            view->begin(1);
            view->resize(800, 600);
            view->show();
            std::cout<<pattern_id<<std::endl;
            }
            int MainWindow::getid()
            {
            return pattern_id;
            }
            MainWindow::~MainWindow()
            {
            delete ui;
            if(view==NULL)
            {
            std::cout<<"view is null"<<std::endl;
            }
            if(view!=NULL){
            std::cout<<"view object deleted"<<std::endl;
            delete view;}
            }
            @

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sunil.nair
              wrote on last edited by
              #6

              Basically, can I use a class private member in a public slot

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                Try this:
                @
                // constructor:
                view = 0;

                // ...
                void MainWindow::pattern1()
                {
                view=new CubeView;
                pattern_id=2;
                view->begin(1);
                view->resize(800, 600);
                view->show();
                std::cout<<pattern_id<<std::endl;
                }
                @

                (Z(:^

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="sunil.nair" date="1418985874"]Basically, can I use a class private member in a public slot[/quote]

                  Yes. Slots work the same as standard C++ methods.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sunil.nair
                    wrote on last edited by
                    #9

                    sierdzio:thanks for your response. but, view is a window. if i put it in a slot, wouldn't it initialize again and again

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sunil.nair
                      wrote on last edited by
                      #10

                      Also, it gives me the same error:

                      ASSERT failure in QGLPainter: "begin() has not been called or it failed", file painting\qglpainter.cpp, line 1665
                      Invalid parameter passed to C runtime function.

                      1 Reply Last reply
                      0
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #11

                        Sorry, yes. Here is a corrected code:
                        @
                        if (view == 0) {
                        view = new CubeView;
                        }
                        @

                        (Z(:^

                        1 Reply Last reply
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #12

                          I do not know anything about this assert, I can't help you with that, sorry. Please ask Qt3D developers.

                          (Z(:^

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            sunil.nair
                            wrote on last edited by
                            #13

                            Oh man! Thank you . really thanks a lot. you are a saviour!!
                            it works now.

                            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