In my project I promoted Qt Designer form class to mainwindow.ui
. promoted ui I named as doublerect.ui
and which containing two QspinBoxes
one is sb_rect_height
and other one is sb_rect_width
. Now I need to pass those spinboex values to mainwindow.there for I created getters
and setters
. But when I access getter
from mainwindow
those values print as this 0 This main weight , 1072693248 This main height
. So please tell me and give me a solution how can I access those spinboxes values from mainwindow
.This my complete code
doublerect.cpp
#include "doublerect.h"
#include "ui_doublerect.h"
#include "mainwindow.h"
#include "qdebug.h"
DoubleRect::DoubleRect(QWidget *parent) :
QWidget(parent),
ui(new Ui::DoubleRect)
{
ui->setupUi(this);
connect(ui->sb_rect_height, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
this, &DoubleRect::setHeight);
connect(ui->sb_rect_width, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
this, &DoubleRect::setWidth);
}
DoubleRect::~DoubleRect()
{
delete ui;
}
int DoubleRect::getWidth() const
{
return width;
}
void DoubleRect::setWidth(int value)
{
width = value;
}
int DoubleRect::getHeight() const
{
return height;
}
void DoubleRect::setHeight(int value)
{
height = value;
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <QColorDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->widgethide->setVisible(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addRect()
{
DoubleRect *obj = new DoubleRect();
int height = obj->getHeight();
int width = obj->getWidth();
qDebug()<< height <<"This main height";
qDebug()<< width <<"This main width";
}
void MainWindow::on_btnRect_clicked()
{
addRect();
}
Methods setHeight
and setWidth
of DoubleRect
class should be in section - private slots.
public:
explicit DoubleRect(QWidget *parent = 0);
~DoubleRect();
int getWidth() const;
int getHeight() const;
public slots:
private slots:
void setHeight(int value);
void setWidth(int value);
EDIT
When addRect
method is called, in constructor of DoubleRect
you assign signal valueChanged()
of SpinBox with your slots - setHeight
and setWidth
, then you want to print width and height using qDebug()
,
but first you need to change value of spinbox in order to slots can be invoked - and width with height can be set.
You cannot create obj
every time you call addRect()
method. This object should be create only one time.
Then signal-slots connection is made, and when values of spinbox are changed, your slots will be called setting height and width correctly.
EDIT 2
You can define DoubleRect
object as member of MainWindow
:
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
Ui::MainWindow *ui;
QGraphicsScene * scene;
QGraphicsView * view;
DoubleRect* obj;
};
create this object in ctor of MainWindow
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//...
obj = new DoubleRect();
}
now, when you change value of SpinBox, slots setWidth/setHeight will be called, and invoking 'addRect' method you should get correct values.
EDIT 3
I have downloaded your project, I use QT 4.8.4, after some modifications it works, so
[1] changes in doublerect.cpp
DoubleRect::DoubleRect(QWidget *parent) :
QWidget(parent),
ui(new Ui::DoubleRect)
{
ui->setupUi(this);
// NEW LINES !!!
connect (ui->sb_rect_height, SIGNAL(valueChanged(int)), this, SLOT(setHeight(int)));
connect (ui->sb_rect_width, SIGNAL(valueChanged(int)), this, SLOT(setWidth(int)));
}
use connect function to make connection between SIGNAL - SLOT
[2] changes in doublerect.h
class DoubleRect : public QWidget
{
Q_OBJECT
public:
explicit DoubleRect(QWidget *parent = 0);
~DoubleRect();
int getWidth() const;
int getHeight() const;
public slots:
private slots: // NEW LINES, these methods must be slots
void setHeight(int value);
void setWidth(int value);
private:
Ui::DoubleRect *ui;
int width;
int height;
};
and the most important things, changes in mainwindow.cpp
void MainWindow::addRect()
{
//DoubleRect *obj = new DoubleRect(); // don't create new object
int height = ui->widgethide->getHeight();
int width = ui->widgethide->getWidth();
qDebug()<< height <<"This main height";
qDebug()<< width <<"This main width";
}
you don't need to create DoubleRect
object, this object exists already, pointer to instance of this object is ui->widgethide
.