[solved] Class object in mainwindow.h



  • 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.


  • Moderators

    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.



  • 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.


  • Moderators

    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.



  • 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;}
    }
    @



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


  • Moderators

    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;
    }
    @


  • Moderators

    [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.



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



  • 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.


  • Moderators

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


  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.