0
votes

I am now trying to create a simple GUI openGL drawing program.

I use a Line2dFactory (subclass of Shape2dFactory, which is a subclass of QWidget) class to receive the values assigned by various input widgets and then wrap them up as a Line2d (subclass of Shape2d) object and send the object to openGL widget for drawing.

The Line2dFactory class looks like:

class Line2dFactory : public Shape2dFacotry
{
    Q_OBJECT
public:
    explicit Line2dFactory(QWidget *parent = 0);
    ~Line2dFactory();

signals:
    // this is a member of Shape2dFacotory. for the purpose of
    // illustration I put it here
    void shape2dSent(Shape2d shape);

public slots:
    // set begin and end points
    void setX1(int i);
    void setY1(int i);
    void setX2(int i);
    void setY2(int i);

    // implement object creation, and emit the created object to display
    void sendLine2d();

private:
    int x1, y1, x2, y2;
    Line2d line2d;
};

I connected set**(int) slots to 4 QSpinBoxes' ValueChanged(int) signals. A push button's clicked() signal is connected to sendLine2d() slot.

After the program fires up, if I change ALL the values in the QSpinBoxes via GUI and click the push button then an object is created and sent to display.

However, if I change not all the values in the spin boxes (the default is 0), the signals of those unchanged spin boxes seems to be not sent. Therefore the initial values of the corresponding points are random int. If the push button is clicked then the program will freeze the computer since it try to compute likely a very long line.

My question is: What signal could I connect the set** slot to to make the factory class know about the initial value of the Qspinbox? And, more generally, for other gui components?

I don't want to assign a value manually since I might forget to change both if I decide to change the default value.

1

1 Answers

0
votes

You can initialize values in the constructor

Line2dFactory(int p_x1, int p_x2, int p_x3, int p_x4, QWidget *parent = 0):
       x1(p_x1),
       x2(p_x2),
       x3(p_x3),
       x4(p_x4)
{
}

then with your spin boxes

QSpinBox sb1;
QSpinBox sb2;
QSpinBox sb3;
QSpinBox sb4;
Line2DFactor factory(sb1.value(), sb2.value(), sb3.value(), sb4.value());

Or the other way around, initialize spin boxes with values provided by factory

QSpinBox sb1;
sb1.setValue(factory.getX1());

valueChanged(int) signal is sent by QSpinBox only if value has been changed and it differs from the old one. AFAIK you could call explicitly sb1.valueChanged(sb1.value()) and it should trigger all connected slots, but it's IMO better and cleaner to initialize Line2DFactory in the constructor.