setStyleSheet(styleSh...-Go服务器开发" />

ZBLOG

QWidget *vg = new QWidget(this); //ui->horizontalLayout_2->addWidget(vg); QString imagePath = "E:/YZYEdu/WhiteBoard/img/123.png"; QString styleSheet = QString("background-image: url(%1);").arg(imagePath); vg->setStyleSheet(styleSh...

要使 vgpNewBoard 具有相同的大小,你可以使用布局管理器来自动调整它们的大小。在上面的代码中,你可以尝试将 vgpNewBoard 放入一个父级容器(例如 QWidget 或 QFrame),并将该容器设置为水平布局。

以下是修改后的示例代码:

// 创建一个父级容器,并设置为水平布局
QWidget* containerWidget = new QWidget(this);
QHBoxLayout* horizontalLayout = new QHBoxLayout(containerWidget);

// 创建 vg 控件,并设置背景图片样式
QWidget* vg = new QWidget(containerWidget);
QString imagePath = "E:/YZYEdu/WhiteBoard/img/123.png";
QString styleSheet = QString("background-image: url(%1);").arg(imagePath);
vg->setStyleSheet(styleSheet);

// 创建 pNewBoard 控件(假设 WhiteBoard 是 QWidget 的子类)
WhiteBoard* pNewBoard = new WhiteBoard(containerWidget, Qt::transparent);
m_lsWhiteBoard.push_back(pNewBoard);
for (auto it = m_lsWhiteBoard.begin(); it != m_lsWhiteBoard.end(); ++it) {
    WhiteBoard* ptr = *it;
    ptr->hide();
}
m_CurrWhiteBoard = pNewBoard;

// 将 vg 和 pNewBoard 添加到水平布局中
horizontalLayout->addWidget(vg);
horizontalLayout->addWidget(m_CurrWhiteBoard);

// 设置伸展因子以使 vg 和 pNewboard 充满父级容器
horizontalLayout->setStretch(0, 1);
horizontalLayout->setStretch(1, 1);

// 将父级容器添加到 ui->horizontalLayout_2
ui->horizontalLayout_2->addWidget(containerWidget);

这样,vgpNewBoard 将具有相同的大小,并且随着父级容器的调整而自动调整大小。请注意,你需要将 WhiteBoard 类替换为你实际使用的控件类。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:setStyleSheet(styleSh...">https://golang.0voice.com/?id=18077

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?