【问题标题】:QFrame is not adjusting its size as per the size adjustment of parent widgetQFrame 没有根据父小部件的大小调整来调整它的大小
【发布时间】:2019-06-19 14:31:42
【问题描述】:

我创建了一个小部件类。此类将维护 QFrame 类型的 child_1(QWidget 类型)以及 QGridLayout 类型的 child_2。 child_2 会将 child_1 添加到自身。 child_1 将有一个 QGridLayout 类型的子级,最终 QGridLayout 将有一些按钮。

*我无法根据父窗口小部件的调整来调整 QFrame child(child_1) 的大小。 另外,我正在尝试设置父 QFrame 子的大小。即使这没有发生。 我尝试过使用 setFrameRect()、setGeometry() 和 resize() 方法。 我已经实现了 evenhandler resizeEvent(QResizeEvent event)。但是在应用程序运行时,我正在尝试调整小部件的大小,但发现日志没有打印。

ma​​in.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")
}

【问题讨论】:

    标签: c++ qt qframe


    【解决方案1】:

    我设法解决了这个问题。

    我错过了在父窗口小部件中实现事件处理程序 resizeEvent(QResizeEvent *event)。 我实现了同样的方法并调用了 FrameDemo 的 resizeEvent。在这个事件处理程序中,我调用了 setGeometry()。

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 2010-10-26
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      相关资源
      最近更新 更多