Search code examples
c++qtqt-signalsslot

How do I correctly use the signal/slot system from qt


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?


Solution

  • 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)));