0
votes

I created a widget class. This class will be maintaining child_1 of type QFrame(which is of type QWidget) and also a child_2 of type QGridLayout . child_2 will be adding child_1 to itself. The child_1 will be having a child of type QGridLayout and ultimately QGridLayout will be having some push buttons.

*I am unable to adjust the size of QFrame child(child_1)as per the adjustment of parent widget. Also, I am trying to set the size of QFrame child that of parent. Even this isn't happening. I have tried using methods setFrameRect(), setGeometry() and resize(). I have implemented evenhandler resizeEvent(QResizeEvent event). But while application is running, I am trying to resize the widget and I see that log isn't printing.

main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.h

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
    QGridLayout* p_mPanelLayout; // Instance of QGridLayout.
    FrameDemo* framePtr;
};

widget.cpp

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    p_mPanelLayout = new QGridLayout(this);
    framePtr = new FrameDemo(this);
    framePtr->createPushButtonUtility();

    p_mPanelLayout->addWidget(framePtr, 0, 0, Qt::AlignCenter);
}

Widget::~Widget()
{
    delete ui;
}

framedemo.h

#define LOG(message){\
qDebug()<< __LINE__<<__FUNCTION__<<message<<"\n";\
 }

class FrameDemo : public QFrame
{
    Q_OBJECT
public:
    FrameDemo(QWidget *parent = 0);
    void createPushButtonUtility();
private:
    QGridLayout* m_pLayout;
protected:
    void resizeEvent(QResizeEvent * event);
};

framedemo.cpp

FrameDemo::FrameDemo(QWidget *parent):QFrame(parent)
{
    m_pLayout = new QGridLayout(this);
//    m_pLayout->setRowStretch(0, parent->width());

    QRect defaultRect = rect();
    QSize currentSize = size();
    LOG(currentSize.height())
    LOG(currentSize.width())

    int xP1 = 0;
    int yP1 = 0;
    int xP2 = 0;
    int yP2 = 0;

    defaultRect.getCoords(&xP1, &yP1, &xP2, &yP2);
    LOG(xP1)
    LOG(yP1)
    LOG(xP2)
    LOG(yP2)
    LOG(parent->width())
    LOG(defaultRect.height())
//            QRect newRect = QRect(xP1,yP1,parent->width(),defaultRect.height());
//            QRect newRect = QRect(0, 0, 0, defaultRect.height());
//            setFrameRect(newRect);
            resize(QSize(400, currentSize.height()));

            setFrameShape(QFrame::Box);
//            setGeometry(0, 0, parent->width(), defaultRect.height());
}


void FrameDemo::createPushButtonUtility()
{
    QPushButton *readButton = new QPushButton("Read", this);
    m_pLayout->addWidget(readButton, 0, 0, Qt::AlignCenter);
    QPushButton *writeButton = new QPushButton("Write", this);
    m_pLayout->addWidget(writeButton, 0, 1, Qt::AlignCenter);
    QPushButton *updateButton = new QPushButton("Update", this);
    m_pLayout->addWidget(updateButton, 0, 2, Qt::AlignCenter);
}

void FrameDemo::resizeEvent(QResizeEvent *event)
{
    LOG("resize")
}
1

1 Answers

0
votes

I managed to fix the problem.

I missed to implement the event handler resizeEvent(QResizeEvent *event) in parent widget. I implemented the same and called the resizeEvent of FrameDemo. Inside this event handler, I called setGeometry().