qt删除布局代码 [英] qt remove layouts code

查看:158
本文介绍了qt删除布局代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在QT中的经验不足1天(这就是为什么我对其了解不多),我有一个窗口,其中充满了按布局组织的信息(标签,文本,按钮等).

I have less than 1 day of experience in QT (that's why I do not know much of it) I have a window full of information (labels, text, buttons, etc) organized by layouts.

我需要在按下一个按钮后隐藏窗口中的所有组件(我已经这样做了),除了一个标签,该标签应该增加到几乎整个窗口的大小

I need that after I press one button, all of the components in a window be hidden (which I already did) except for one label which should increase to barely the size of the whole window

尽管我尝试使用代码修改"geometry"属性(使用代码),但隐藏的布局不允许增加标签.我还考虑过使用中断布局的选项,但是标签失去了动态性.有人可以推荐我做什么吗?谢谢.

Despite I tried modifying the "geometry" attribute (with code) the hidden layouts do not let the label to be increased. I thought also of using the option of layout breaking, but the label losses its dynamism. Could anyone please recommend me anything to do? Thanks.

以前有人做过这样的事情吗?谢谢.

Has anyone done something like this before. Thanks.

推荐答案

我曾经提供了 SO:Qt-如何创建的答案图像会随窗口缩放并保持宽高比吗?.实际目的是使用原始宽高比在QLabel中缩放图像以消耗最大可用尺寸.

I once provided an answer to SO: Qt - How to create Image that scale with window, and keeps aspect ratio?. The actual intention was to scale an image in a QLabel with original aspect ratio to consume maximum available size.

但是,我得到的反馈是,当在QGridLayout中使用我的Label时,建议的解决方案将无法正常工作. (这听起来与OP的问题非常相似.)因此,我修改了示例以重现该问题,并进行了一些修改.对我来说,主窗口的调整大小事件似乎在QGridLayout中处理,但仅部分影响布局的图像标签. (应用了收缩,但没有应用收缩.)幸运的是,我找到了一个非常简单的解决方法:将QLabel设置为非空框架可以解决此问题.我在 woboq上查看了源代码.org .我希望能得到一个提示,提示更改后的框架样式将激活什么(将此作为修复我的调整大小问题).最后,我不够耐心,把它放在一旁.

However, I got the feedback that the suggested solution would not work properly when my Label would be used in a QGridLayout. (This sounds very similar to the issue of the OP.) Hence, I modified the sample to reproduce the issue and fiddled a little bit around with. For me, it seems that resize events of the main window are processed in the QGridLayout but affect layouted image label only partially. (Shrinking is applied but growing not.) Fortunately, I found a very simple work-around: Setting a non-empty frame to the QLabel solved the problem. I had a look into the source code on woboq.org. I hoped to get a hint what the changed frame style would activate (to apply this as fix for my resize issue). Finally, I was not patient enough and put it aside.

QGridLayout大小调整问题中,除了此QLabel之外,更改小部件的可见性还应引起适当的重新布局.我更喜欢显示/隐藏(而不是删除和更新),因为它无疑更易于实现,更高效且不易出错.

Beside of this QLabel in a QGridLayout resize issue, changing the visibility of widgets should cause a proper re-layout. I would prefer show/hide (instead of delete and re-new) as this is surely easier to implement, more efficient, and less error-prone.

我采用了旧的示例代码,并添加了一个工具按钮,该按钮可用于切换某些布局小部件的可见性:

I took the old sample code and added a tool button which can be used to toggle the visibilty of some of the layouted widgets:

// Qt header:
#include <QtWidgets>

class LabelImage: public QLabel {

  private:
    QPixmap _qPixmap, _qPixmapScaled;

  public:
    void setPixmap(const QPixmap &qPixmap) { setPixmap(qPixmap, size()); }

  protected:
    virtual void resizeEvent(QResizeEvent *pQEvent);

  private:
    void setPixmap(const QPixmap &qPixmap, const QSize &size);
};

void LabelImage::resizeEvent(QResizeEvent *pQEvent)
{
  QLabel::resizeEvent(pQEvent);
  setPixmap(_qPixmap, pQEvent->size());
}

void LabelImage::setPixmap(const QPixmap &qPixmap, const QSize &size)
{
  _qPixmap = qPixmap;
  _qPixmapScaled = _qPixmap.scaled(size, Qt::KeepAspectRatio);
  QLabel::setPixmap(_qPixmapScaled);
}

int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  // main application
  QApplication app(argc, argv);
  // setup GUI
  QMainWindow qWin;
  QToolBar qToolbar;
  QAction qCmdTgl(QString::fromUtf8("Decoration"));
  qCmdTgl.setCheckable(true);
  qCmdTgl.setChecked(true);
  qToolbar.addAction(&qCmdTgl);
  qWin.addToolBar(&qToolbar);
  QGroupBox qBox;
  QGridLayout qGrid;
  // a macro for the keyboard lazy:
#define Q_LBL_WITH_POS(ROW, COL) \
  QLabel qLbl##ROW##COL(QString::fromLatin1(#ROW", "#COL)); \
  /*qLbl##ROW##COL.setFrameStyle(QLabel::Raised | QLabel::Box);*/ \
  qGrid.addWidget(&qLbl##ROW##COL, ROW, COL, Qt::AlignCenter)
  Q_LBL_WITH_POS(0, 0);
  Q_LBL_WITH_POS(0, 1);
  Q_LBL_WITH_POS(0, 2);
  Q_LBL_WITH_POS(1, 0);
  LabelImage qLblImg;
  qLblImg.setFrameStyle(QLabel::Raised | QLabel::Box);
  qLblImg.setAlignment(Qt::AlignCenter);
  //qLblImg.setMinimumSize(QSize(1, 1)); // seems to be not necessary
  qLblImg.setSizePolicy(
    QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
  QPixmap qPM;
  if (qPM.load("cats.jpg")) qLblImg.setPixmap(qPM);
  else {
    qLblImg.setText(
      QString::fromLatin1("Sorry. Cannot find file 'cats.jpg'."));
  }
  qGrid.addWidget(&qLblImg, 1, 1, Qt::AlignCenter);
  qGrid.setRowStretch(1, 1); // tell QGridLayout to stretch this cell...
  qGrid.setColumnStretch(1, 1); // ...prior to other cells (w/ stretch 0)
  Q_LBL_WITH_POS(1, 2);
  Q_LBL_WITH_POS(2, 0);
  Q_LBL_WITH_POS(2, 1);
  Q_LBL_WITH_POS(2, 2);
  qBox.setLayout(&qGrid);
  qWin.setCentralWidget(&qBox);
  qWin.show();
  // install signal handlers
  QObject::connect(&qCmdTgl, &QAction::triggered,
    [&](bool on) {
      qLbl00.setVisible(on); qLbl01.setVisible(on); qLbl02.setVisible(on);
      qLbl10.setVisible(on);                        qLbl12.setVisible(on);
      qLbl20.setVisible(on); qLbl21.setVisible(on); qLbl22.setVisible(on);
    });
  // run application
  return app.exec();
}

我在Windows 10的VS2013中进行了编译和测试:

I compiled and tested in VS2013 on Windows 10:

切换装饰工具按钮后:

注意:

出于好奇,我评论了改变框架样式的那一行

Out of curiosity, I commented the line which changes the frame style

qLblImg.setFrameStyle(QLabel::Raised | QLabel::Box);

再次,调整图像大小无法正常工作.

and again, resizing of image didn't work properly anymore.

这篇关于qt删除布局代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆