0

So I want to send the height and width of my QScrollArea to my QWidget (both are custom classes that derive from these two) but I just don't get it to wrok.

customScrollArea *scrollArea;
RenderArea *scrollAreaWidgetContents;

void customScrollArea::resizeEvent(QResizeEvent* event)
{
   QScrollArea::resizeEvent(event);
   emit sizeChanged(width(),height());
}

MyGUI::MyGUI(QWidget *parent) :
    QWidget(parent),
ui(new Ui::MyGUI)
{
   ui->setupUi(this);
   connect(ui->scrollArea, SIGNAL(customScrollArea::sizeChanged(int,int)), ui->scrollAreaWidgetContents, SLOT(RenderArea::setSize(int,int)));
}

void RenderArea::setSize(int x, int y)
{
   scrollwidth = x;
   scrollheight = y;
}

when I compile, I get the error "QScrollArea::sizeChanged(int,int) in mygui.cpp:10" but shouldn't it be CustomScrollArea instead of QScrollArea?

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
LeonU
  • 11
  • 3

2 Answers2

2

The macros SIGNAL and SLOT work based on textual comparison of names, and are thus sensitive to correct qualification. The signal/slot name must never be qualified, and any types must be qualified exactly the same as they were in their declaration. So change your code to this:

connect(ui->scrollArea, SIGNAL(sizeChanged(int,int)), ui->scrollAreaWidgetContents, SLOT(setSize(int,int)));
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • what about the new syntax? `connect(ui->scrollArea, &customScrollArea::sizeChanged, ui->scrollAreaWidgetContents, &RenderArea::setSize);` – easysaesch Jan 04 '18 at 21:59
  • i did that but now i still get the error in my console Qobject::connect: No such signal QScrollArea::sizeChanged(int,int) in mygui.cpp:10 – LeonU Jan 04 '18 at 22:01
  • @bob I haven't had a chance to move to Qt 5 yet, so I am not familiar with the new syntax (I just know there *is* a new syntax). – Angew is no longer proud of SO Jan 05 '18 at 07:45
  • @LeonU Which one fails like this for you, the answer's code or that by bob? – Angew is no longer proud of SO Jan 05 '18 at 07:45
  • @Angew i tried yours and I think that the main error would be that i get the error "QScrollArea::sizeChanged(int,int)" and not "CustomScrollArea::sizeChanged(int,int)" even when i used bobs solution I get the error QObject::connect: No such signal QScrollArea::&scrollArea::sizeChanged(int,int) in mygui.cpp::10 – LeonU Jan 05 '18 at 10:41
  • @LeonU Does `ui->scrollArea` actually point to an instance of `CustomScrollArea` at runtime? Have you promoted the widget in Qt Designer? – Angew is no longer proud of SO Jan 05 '18 at 10:45
  • @Angew Yes it does. At least it does say so in my Visual Studio Window on the bottom left where i can check the variables if i set a breakpoint. And the the "scrollArea" is an instance of the class CustomScrollArea according to Qt Designer – LeonU Jan 05 '18 at 11:04
  • @Angew Alright i managed to fix my Problem by changing the update function of my main QWidget to void MyGUI::resizeEvent(QResizeEvent *event) { QWidget::resizeEvent(event); ui->scrollAreaWidgetContents->setSize(ui->scrollArea->width(), ui->scrollArea->height()); } but still i'd very much like to know what i did wrong there if you know it. – LeonU Jan 05 '18 at 11:47
  • @LeonU And is your class processed by `moc`, and does it contain the `Q_OBJECT` macro? – Angew is no longer proud of SO Jan 05 '18 at 13:02
  • @Angew i forgot the Q_OBJECT thank you very much. – LeonU Jan 05 '18 at 13:42
  • @LeonU And that is why questions are supposed to contain a [mcve]. – Angew is no longer proud of SO Jan 05 '18 at 13:47
0

Here some example. There are 2 Widget: Widget and Widget2. When resizing Widget w the new size is displayed in Widget2 w2. In this example this signal and slot of this two widgets are connected via:

QObject::connect(&w, &Widget::resizeSignal,
                   &w2, &Widget2::onResizeSignal);

Hope this helps.

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class Widget : public QWidget
{
  Q_OBJECT
public:
  Widget(QWidget *parent = 0);
  ~Widget();

protected:
  void resizeEvent(QResizeEvent* e) override;

signals:
  void resizeSignal(int w, int h);

};

class Widget2 : public QWidget
{
  Q_OBJECT
public:
  Widget2(QWidget *parent = 0);
  ~Widget2();

public slots:
  void onResizeSignal(int w, int h);

protected:
  void paintEvent(QPaintEvent*) override;

private:
  int width = 0;
  int height = 0;
};

#endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include <QPainter>
#include <QResizeEvent>

Widget::Widget(QWidget *parent)
  : QWidget(parent)
{}

Widget::~Widget()
{}

void Widget::resizeEvent(QResizeEvent* e)
{
    emit resizeSignal(e->size().width(), e->size().height());
    QWidget::resizeEvent(e);
}

Widget2::Widget2(QWidget *parent)
  : QWidget(parent)
{}


Widget2::~Widget2()
{}

void Widget2::onResizeSignal(int w, int h)
{
    width = w;
    height = h;
    repaint();
}


void Widget2::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::red));
    painter.drawText(QPoint(50, 50),
                     QString("(w,h) = (%1, %2)").arg(QString::number(width), QString::number(height)));
}

main.cpp:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Widget w;
  Widget2 w2;

  QObject::connect(&w, &Widget::resizeSignal,
                   &w2, &Widget2::onResizeSignal);


  w.show();
  w2.show();

  return a.exec();
}
StPiere
  • 4,113
  • 15
  • 24