Passing parameters to slots



  • Hey there,

    I have a slot that takes an int as a parameter. However when I call the slot and put a number into the parenthesis, it gives me this error:

    @QObject::connect: No such slot browseTab::loadBar(25)@

    how do I pass a value into the function?



  • have u defined slot in header file correctly ?? and are u defined connect method for calling this slot ??

    means if u defined connect method then slot's passing parameter type should be same as signal's parameter type.



  • Can you show, how do you make connection?



  • ok here is my connect:
    @connect(webView, SIGNAL(loadProgress(int)), SLOT(loadBar(25)));@
    I need to pass a value of 25 to the loadBar function (to show the QWebView is 25% loaded)



  • Well if you use Qt4:

    1. Using two pairs of signal and slot
    2. Using "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html#details

    In Qt5 you also can use functors "Signal and slot":http://qt-project.org/doc/qt-5/signalsandslots.html
    something like that:
    @connect(sender, &QObject::loadProgress, ={ this->loadBar(25); });@

    but just curious why you want always show loading progress as 25%?


  • Moderators

    [quote author="nicky j" date="1393303099"]ok here is my connect:
    @connect(webView, SIGNAL(loadProgress(int)), SLOT(loadBar(25)));@
    I need to pass a value of 25 to the loadBar function (to show the QWebView is 25% loaded)[/quote]The signal parameter is copied into the slot parameter.

    When the QWebView emits the loadProgress() signal, it will carry an int value. Your loadBar() slot will automatically receive that value.

    Write this instead:
    @connect(webView, SIGNAL(loadProgress(int)),
    this, SLOT(loadBar(int)));@

    if you want to call loadBar() without waiting for a signal, just call it like a regular function:
    @this->loadBar(25);@



  • hello nlcky ,

    here you are passing 25 directly to your slot which will give an error .
    Donn't pass 25 in direct manner.



  • Ok thanks for the help!



  • ok so I changed the connect function to:
    @connect(webView, SIGNAL(loadProgress(int)), SLOT(loadBar(int)));@
    It compiles, but doesn't seem to be called whenever the webView is loading.
    Im starting to think its trouble with the slot itself:

    @void browseTab::loadBar(int progress)
    {
    if(progress <= 25)
    {
    SearchBar->setStyleSheet(
    "border-right:2px solid blue;"
    );
    }
    if(progress == 50)
    {
    SearchBar->setStyleSheet(
    "border-right:2px solid blue;"
    "border-bottom:2px solid blue;"
    );
    }
    if(progress == 75)
    {
    SearchBar->setStyleSheet(
    "border-right:2px solid blue;"
    "border-bottom:2px solid blue;"
    "border-left:2px solid blue;"
    );
    }
    if(progress == 100)
    {
    SearchBar->setStyleSheet(
    "border-right:2px solid blue;"
    "border-bottom:2px solid blue;"
    "border-left:2px solid blue;"
    "border-top:2px solid blue;"
    );
    }
    if(progress == 101)
    {
    SearchBar->setStyleSheet(
    "border-right:2px solid black;"
    "border-bottom:2px solid black;"
    "border-left:2px solid black;"
    "border-top:2px solid black;"
    );
    }
    }@

    Basically I'm using the searchBar as a progress bar by changing the color of the sides as the page loads. Obviously it is not working. Ideas?


  • Moderators

    To check if a slot is called, stick a qDebug() inside it.

    Also, you check if progress equals a particular value. Your slot won't do anything if it receives 49 or 51. It's best not to assume that every value between 1 and 100 will be received.



  • yes int progress = 0. Should I change that? If not every value will be called, what should I do in the if() statements that are called when it reaches certain points? Is there a range of values I should call? if so, how do I do that?


  • Moderators

    You don't get to choose which values you receive -- QWebView is responsible for that.

    Use qDebug() to print the value of progress each time your slot is called. That should help you decide what to do.



  • ok I removed the default value from int progress. There is still nothing happening


  • Moderators

    Use qDebug() to print progress. (Removing the default value should have no effect whatsoever)



  • where do I place qDebug()?


  • Moderators

    What debugging techniques are you familiar with? Have you used printf() before?



  • nope Ive never used printf() or qDebug() before


  • Moderators

    See "QDebug | Basic Use":http://qt-project.org/doc/qt-5/QDebug.html#basic-use for an example.

    It is a very simple but very useful technique to check your program. If you put qDebug() in a function, it will print a message in your Application Output pane* every time that function is called. If the message doesn't appear, that means your function hasn't been called. once you play with it, I'm sure you can think of other uses for qDebug().

    Use qDebug() to:

    Check if loadBar() is called

    Examine the value of progress inside loadBar()

    *(If you don't know where the Application Output pane is, search the "Qt Creator | User Interface":http://qt-project.org/doc/qtcreator-3.0/creator-quick-tour.html#application-output page)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.