Scrollarea, Funktionen und Instanzen

Du bist neu in der Welt von C++? Dann schau hier herein!
Antworten
MHage
Beiträge: 6
Registriert: 4. Oktober 2022 20:54

Scrollarea, Funktionen und Instanzen

Beitrag von MHage »

Hallo zusammen,

Ich versuche mich gerade an einem kleinen Projekt um mit Qt das erste Mal unter Linux etwas zu schreiben.
In der Vergangenheit habe ich Embarcadero C++ verwendet. Das ist allerdings jetzt schon eher Jahre als Monate her.
Scheinbar viel zu lange, denn es klemmt gerade an den Basics...

Aufgabe ist, eine QMessageBox Scrollbar zu machen. Nach einigem suchen im Netz habe ich einen Snippet gefunden, von dem denke, dass
er genau das macht, was ich möchte.

Code: Alles auswählen

#ifndef SCROLLMESSAGEBOX_H
#define SCROLLMESSAGEBOX_H

#include <QScrollArea>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLabel>
#include <QMessageBox>
#include <QString>
#include <QLayout>
#include <QStyle>
#include <QApplication>
#include <QPushButton>
#include <QDesktopWidget>

//#pragma once

// ScrollMessageBox
// This class is just like QMessageBox, the only difference is that we put the displayed
// text inside a scroll area so that longer messages can be shown.

class ScrollMessageBox : public QDialog
{
  Q_OBJECT

public:
  ScrollMessageBox(QMessageBox::Icon icon, QString const& title, QString const& text,
    QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok, QWidget* parent = 0);

  void setDefaultButton(QDialogButtonBox::StandardButton button);

  static QDialogButtonBox::StandardButton critical(QWidget* parent, QString const& title, QString const& text, QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok, QDialogButtonBox::StandardButton defaultButton = QDialogButtonBox::NoButton);
  static QDialogButtonBox::StandardButton information(QWidget* parent, QString const& title, QString const& text, QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok, QDialogButtonBox::StandardButton defaultButton = QDialogButtonBox::NoButton);
  static QDialogButtonBox::StandardButton question(QWidget* parent, QString const& title, QString const& text, QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok, QDialogButtonBox::StandardButton defaultButton = QDialogButtonBox::NoButton);
  static QDialogButtonBox::StandardButton warning(QWidget* parent, QString const& title, QString const& text, QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok, QDialogButtonBox::StandardButton defaultButton = QDialogButtonBox::NoButton);

  void showEvent ( QShowEvent * event ) override;

private:
  QPixmap standardIcon(QMessageBox::Icon icon);
  void setDefaultButton(QPushButton *button);
  void updateSize();

  QLabel *label;
  QDialogButtonBox *buttonBox;

private Q_SLOTS:
  void handle_buttonClicked(QAbstractButton *button);
};



#endif // SCROLLMESSAGEBOX_H

Code: Alles auswählen

#include "scrollmessagebox.h"

ScrollMessageBox::ScrollMessageBox(QMessageBox::Icon icon, QString const& title,
  QString const& text, QDialogButtonBox::StandardButtons buttons /*= QDialogButtonBox::Ok*/,
  QWidget* parent /*= 0*/) :
  QDialog(parent, Qt::Dialog | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
{
  QLabel *iconLabel;
  QScrollArea *scroll;

  label = new QLabel;
  label->setTextInteractionFlags(Qt::TextInteractionFlags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, this)));
  label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  label->setOpenExternalLinks(true);
  label->setContentsMargins(2, 0, 0, 0);
  label->setIndent(9);

  scroll = new QScrollArea(this);
  scroll->setGeometry(QRect(10, 20, 560, 430));
  scroll->setWidget(label);
  scroll->setWidgetResizable(true);

  iconLabel = new QLabel;
  iconLabel->setPixmap(standardIcon((QMessageBox::Icon)icon));
  iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

  buttonBox = new QDialogButtonBox(buttons);
  buttonBox->setCenterButtons(style()->styleHint(QStyle::SH_MessageBox_CenterButtons, 0, this));
  QObject::connect(buttonBox, SIGNAL(clicked(QAbstractButton*)),
    this, SLOT(handle_buttonClicked(QAbstractButton*)));

  QGridLayout *grid = new QGridLayout;

  grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop);
  grid->addWidget(scroll, 0, 1, 1, 1);
  grid->addWidget(buttonBox, 1, 0, 1, 2);
  grid->setSizeConstraint(QLayout::SetNoConstraint);
  setLayout(grid);

  if (!title.isEmpty() || !text.isEmpty())
  {
    setWindowTitle(title);
    label->setText(text);
  }
  setModal(true);
}

QPixmap ScrollMessageBox::standardIcon(QMessageBox::Icon icon)
{
  QStyle *style = this->style();
  int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
  QIcon tmpIcon;
  switch (icon)
  {
    case QMessageBox::Information:
      tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation, 0, this);
      break;
    case QMessageBox::Warning:
      tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning, 0, this);
      break;
    case QMessageBox::Critical:
      tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical, 0, this);
      break;
    case QMessageBox::Question:
      tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion, 0, this);
    default:
      break;
  }
  if (!tmpIcon.isNull())
  {
    return tmpIcon.pixmap(iconSize, iconSize);
  }
  return QPixmap();
}

void ScrollMessageBox::handle_buttonClicked(QAbstractButton *button)
{
  int ret = buttonBox->standardButton(button);
  done(ret);
}

void ScrollMessageBox::setDefaultButton(QPushButton *button)
{
  if (!buttonBox->buttons().contains(button))
    return;
  button->setDefault(true);
  button->setFocus();
}

void ScrollMessageBox::setDefaultButton(QDialogButtonBox::StandardButton button)
{
  setDefaultButton(buttonBox->button(button));
}

void ScrollMessageBox::showEvent(QShowEvent *e)
{
  updateSize();
  QDialog::showEvent(e);
}

void ScrollMessageBox::updateSize()
{
  if (!isVisible())
    return;

  QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size();

  int hardLimit = qMin(screenSize.width() - 480, 1000); // can never get bigger than this
  // on small screens allows the messagebox be the same size as the screen
  if (screenSize.width() <= 1024)
    hardLimit = screenSize.width();

  layout()->activate();
  int width = layout()->totalMinimumSize().width();

  {
    QFontMetrics fm(QApplication::font("QWorkspaceTitleBar"));
    int windowTitleWidth = qMin(fm.width(windowTitle()) + 50, hardLimit);
    if (windowTitleWidth > width)
      width = windowTitleWidth;
  }



  hardLimit = qMin(screenSize.height() - 200, 1000); // can never get bigger than this

  int height;
  {
    QFontMetrics fm(label->font());
    int lw = label->width()+5;
    height = qMin((fm.width(label->text()) + 50)/lw, hardLimit);
  }

  resize(width, height);
}


QDialogButtonBox::StandardButton ScrollMessageBox::critical(QWidget* parent, QString const& title, QString const& text,
  QDialogButtonBox::StandardButtons buttons, QDialogButtonBox::StandardButton defaultButton)
{
  ScrollMessageBox box(QMessageBox::Critical, title, text, buttons, parent);
  box.setDefaultButton(defaultButton);
  return static_cast<QDialogButtonBox::StandardButton>(box.exec());
}

QDialogButtonBox::StandardButton ScrollMessageBox::information(QWidget* parent, QString const& title, QString const& text,
  QDialogButtonBox::StandardButtons buttons, QDialogButtonBox::StandardButton defaultButton)
{
  ScrollMessageBox box(QMessageBox::Information, title, text, buttons, parent);
  box.setDefaultButton(defaultButton);
  return static_cast<QDialogButtonBox::StandardButton>(box.exec());
}

QDialogButtonBox::StandardButton ScrollMessageBox::question(QWidget* parent, QString const& title, QString const& text,
  QDialogButtonBox::StandardButtons buttons, QDialogButtonBox::StandardButton defaultButton)
{
  ScrollMessageBox box(QMessageBox::Question, title, text, buttons, parent);
  box.setDefaultButton(defaultButton);
  return static_cast<QDialogButtonBox::StandardButton>(box.exec());
}

QDialogButtonBox::StandardButton ScrollMessageBox::warning(QWidget* parent, QString const& title, QString const& text,
  QDialogButtonBox::StandardButtons buttons, QDialogButtonBox::StandardButton defaultButton)
{
  ScrollMessageBox box(QMessageBox::Warning, title, text, buttons, parent);
  box.setDefaultButton(defaultButton);
  return static_cast<QDialogButtonBox::StandardButton>(box.exec());
}

Soweit, so gut, aber mit dem Aufrufen hapert es irgendwie und ich sehe das Problem einfach nicht...

Code: Alles auswählen

	ScrollMessageBox msgBox;
        msgBox.setStyleSheet("QLabel{min-height: 50px; max-height: 800px; min-width: 800px; font-size: 14px;}");
        msgBox.setWindowTitle(title);
        //msgBox->setText(message);
        msgBox.setDefaultButton(QDialogButtonBox::Ok);
        msgBox.setWindowFlags(Qt::WindowStaysOnTopHint);
        msgBox.setWindowModality(Qt::ApplicationModal);            
        msgBox.exec();

Gleich die erste Zeile bringt folgenden Fehler:


einlassberechnung.cpp:85: Fehler: no matching function for call to ‘ScrollMessageBox::ScrollMessageBox()’
einlassberechnung.cpp: In member function ‘void EinlassBerechnung::on_pushButton_clicked()’:
einlassberechnung.cpp:85:26: error: no matching function for call to ‘ScrollMessageBox::ScrollMessageBox()’
85 | ScrollMessageBox msgBox;
| ^~~~~~


gefolgt von :

einlassberechnung.cpp:85: Fehler: no matching constructor for initialization of 'ScrollMessageBox'

scrollmessagebox.h:22: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided

Und dann auch noch das Problem, dass ich nicht weiß wie ich meinen message -Text übergeben kann, weil er setText nicht kennt.. :roll:

Wäre super, wenn mich jemand etwas erleuchten könnte...


VG
Matze
MHage
Beiträge: 6
Registriert: 4. Oktober 2022 20:54

Re: Scrollarea, Funktionen und Instanzen

Beitrag von MHage »

Hallo,

ich habe dann doch geschafft die dunkle Brille abzunehmen... ;)

Es funktioniert jetzt wie es soll. :D
Antworten