User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Qt4/C++ Refer to a QMainWindow object from a subclass

Thu Jan 29, 2015 7:16 pm

Hello,

I've subclassed a QScrollArea and I am trying to resize a QFrame on the QMainWinow by dragging an edge of my subclass object.
I can resize the subclassed object but can't resize the QFrame (parent QMainWindow).

Perhaps some body can point out what I'm going wrong?
Thanks in advance.

Regards

Code: Select all

class menu : public QMainWindow
{
    Q_OBJECT

public:
    menu(QWidget *parent = 0);
    ~menu();
    QFrame *f2;;

private:
    QScrollArea  sa;
    etc...

Code: Select all

menu::menu(QWidget *parent) : QMainWindow(parent)
{
    QScrollArea *sa = new myScrollArea(this);
    etc...

Code: Select all

class myScrollArea : public QScrollArea
{
    Q_OBJECT

public:
    myScrollArea(QWidget *parent = 0);
    ~myScrollArea();

protected slots:
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent();

private:
    QRect rect1; //sa
    QRect rect2; //f2
    bool  moveInProgress;
};

Code: Select all

void myScrollArea::mousePressEvent(QMouseEvent *e)
{
    menu m1; //instansiate

    moveInProgress = false;
    if ((e->buttons() == Qt::LeftButton) && (e->x() < 3)) {
        moveInProgress = true;
        rect1 = geometry(); //sa
        rect2 = m1.f2->geometry(); //f2 left
    }
}

void myScrollArea::mouseMoveEvent(QMouseEvent *e)
{
    menu m1; //instansiate

    if(e->x() < 3) setCursor(QCursor(Qt::SizeHorCursor));
    else setCursor(QCursor(Qt::ArrowCursor));

    if (moveInProgress) {
        if ((e->buttons() == Qt::LeftButton)) {
            //**********
            m1.f2->setGeometry(3, 100, 3, 100); //**********
            //**********
            qDebug() << "pos = " << e->x();
        }
    }
}

void myScrollArea::mouseReleaseEvent()
{
    moveInProgress = false;
}

laurent
Posts: 333
Joined: Thu Jul 26, 2012 11:24 am

Re: Qt4/C++ Refer to a QMainWindow object from a subclass

Mon Feb 09, 2015 2:14 pm

Hi,

Hope it's not too late.

First, I don't understand how you resize "sa" object with "rect1" one.
You made an error when you declared and instanciated sa :

In header :
QScrollArea * sa;

In code :
sa = new myScrollArea(this); // "QScrollArea *" removed before

Return to “C/C++”