【问题标题】:Scrolling list of labels on QtQt 上的标签滚动列表
【发布时间】:2016-08-04 11:51:35
【问题描述】:

我正在尝试为我的标签创建滚动条。目前,如果用户创建的标签太多,按钮和文本区域的大小会减小,这就是我想创建滚动条的原因,如果标签太多,他们不会改变窗口的外观.

这是我的实际代码:

#include <iostream>
#include <QApplication>
#include <QPushButton>
#include <QLineEdit>
#include <QWidget>
#include <QFormLayout>
#include "LibQt.hpp"

LibQt::LibQt() : QWidget()
{
  this->size_x = 500;
  this->size_y = 500;
  QWidget::setWindowTitle("The Plazza");
  setFixedSize(this->size_x, this->size_y);
  manageOrder();
}

LibQt::~LibQt()
{
}

void LibQt::keyPressEvent(QKeyEvent* event)
{
  if (event->key() == Qt::Key_Escape)
    QCoreApplication::quit();
  else
    QWidget::keyPressEvent(event);
}

void LibQt::manageOrder()
{
  this->converLayout = new QFormLayout;
  this->testline = new QLineEdit;
  this->m_button = new QPushButton("Send");
  this->m_button->setCursor(Qt::PointingHandCursor);
  this->m_button->setFont(QFont("Comic Sans MS", 14));
  this->converLayout->addRow("Order : ", this->testline);
  this->converLayout->addWidget(this->m_button);
  QObject::connect(m_button, SIGNAL(clicked()), this, SLOT(ClearAndGetTxt()));
  CreateLabel("test");
  CreateLabel("test2");
}

void            LibQt::CreateLabel(std::string text)
{
  QString qstr = QString::fromStdString(text);

  this->label = new QLabel(qstr);
  this->converLayout->addWidget(this->label);
  this->setLayout(converLayout);
}

std::string     LibQt::ClearAndGetTxt()
{
  QString txt = this->testline->text();

  if (!txt.isEmpty())
    {
      this->usertxt = txt.toStdString();
      std::cout << this->usertxt << std::endl;
      this->testline->clear();
      CreateLabel(this->usertxt);
      return (this->usertxt);
    }
  return (this->usertxt);
}

std::string     LibQt::getUsertxt()
{
  return (this->usertxt);
}

这是 .hpp :

#ifndef _LIBQT_HPP_
#define _LIBQT_HPP_

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
#include <QKeyEvent>

class   LibQt : public QWidget
{
  Q_OBJECT

public:
  LibQt();
  ~LibQt();
  void manageOrder();
  std::string getUsertxt();
  void keyPressEvent(QKeyEvent *event);
  void keyPressEventEnter(QKeyEvent *event);
  void CreateLabel(std::string text);
public slots:
  std::string ClearAndGetTxt();
protected:
  int   size_x;
  int   size_y;
  QPushButton *m_button;
  QLineEdit *testline;
  std::string usertxt;
  QLabel *label;
  QFormLayout *converLayout;
};

#endif /* _LIBQT_HPP_ */

【问题讨论】:

    标签: c++ qt label scrollbar


    【解决方案1】:

    有不同的解决方案,具体取决于您想要什么

    1. QTextEdit 是用于可滚动文本的 Qt 小部件类。通过关闭文本交互标志、框架样式和取消设置背景颜色,您基本上可以滚动QLabel

    2. QScrollArea 作为更通用的解决方案

    【讨论】:

    • 如果我做一个 qscrollarea,它会占用我所有的小部件,并且 qscrollarea 必须在我的布局中,这是否准确?
    • @Michael003,您必须: 1. 将滚动区域放在小部件内 2. 创建可滚动小部件 3. set 可滚动小部件 4. 将子小部件放入可滚动小部件内
    猜你喜欢
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多