class object as another class member

I create an object of class in another class mainwindow. But, when I initialize this object in the constructorof mainwindow, I get a runtime errror: Invalid parameter passed to C runtime function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #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;
};


#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;
}
Initialize view & ui inside the constructor?
Then do something like:
if(parent != NULL)
I tried to write this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

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


}


It still gives me the following error:

Invalid parameter passed to C runtime function.
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.
You seem to be using "single inheritance with pointer member": https://qt-project.org/doc/qt-5-snapshot/designer-using-a-ui-file.html

What is CubeView?

Why do you add QPushButton in constructor? Why it is not created by the ui-file?
Topic archived. No new replies allowed.