Passing another argument to a SLOT



  • hello
    @connect(QWebVIew(),SIGNAL(titleChanged(QString)),this, SLOT(myslot(QString*,int*)));
    @
    i must pass an intiger with th QString



  • You can connect your signal to a one-arg slot, in the slot, you can call the two-args slot.



  • i didn't understand what have you said.
    ijust want to pass 2 argument the qstring and the int



  • OK, the simply answer is: you can't .



  • As 1+1=2 said - you can't do that. But you can call myslot(QString*,int*) manually from another slot and pass arguments which you need.



  • Or use a qsignalmapper to "parse" the signals into slots with other arguments.



  • I have read the qsignalmapper class in the exemple it show how pass one argument the cliqued signal don't pass an argument
    i want to pass two variable the QString from the titilechanged and the int from my methode


  • Lifetime Qt Champion

    Like the others said, it's not possible to have a slot with more arguments than the signal.

    Use an intermediate slot that will call your function with the necessary parameters.

    Also, are you sure you want to use pointers to QString and int in your slot ?



  • @void mainWind::changetab(int index)
    {
    if(ui->tabWidget->indexOf(ui->newtab)==index)
    {
    addtab();

    }
    

    currentpage(()->settings()->setIconDatabasePath("a");
    url(currentPages()->url());
    connect(currentpage((),SIGNAL(loadProgress(int)),ui->progressBar,SLOT(setValue(int)));
    connect(currentpage((),SIGNAL(loadStarted()),this,SLOT(depart()));
    connect(currentpage((),SIGNAL(loadFinished(bool)),this,SLOT(finish(bool)));
    connect(currentpage((),SIGNAL(loadProgress(int)),this,SLOT(laodPage(int)));
    connect(currentpage((),SIGNAL(titleChanged(QString)),this,SLOT(titl(QString)));
    connect(currentpage((),SIGNAL(urlChanged(QUrl)),this,SLOT(url(QUrl)));
    connect(currentpage((),SIGNAL(iconChanged()),this,SLOT(icon()));

    }@
    @
    QWebView *mainWind:: currentpage()
    {
    if(ui->tabWidget->currentWidget()->findChild<QWebView *>()==0)
    {
    int a=ui->tabWidget->currentIndex()-1;
    return ui->tabWidget->widget(a)->findChild<QWebView *>();

    }
    else
    {
        return ui->tabWidget->currentWidget()->findChild<QWebView *>();
    }
    

    }
    @
    @
    void mainWind::titre(const QString & titre)
    {
    QString titr(titre);
    if(titre.size()>40)
    {
    titr=titre.left(40)+"....";
    }
    ui->tabWidget->setTabText(m_index,titr);
    }
    @
    so when i load a page and I dont change the tab all is ok. but when i leave the page to load and i change the tab. the name of the page load is in the newtab because it is now the current tab. i have the page in a tab and its name in the other tab.
    so thats why i want to pass index of the widget (that well be changing the titl


  • Lifetime Qt Champion

    Then in your slot use sender() to get the widget that emitted the signal and use that to find the right tab to update.



  • thx for answering my question
    if you can please tell me how to use the sender i have read the doc but i didn't understand very well can you please give me a simple example in general.


  • Lifetime Qt Champion

    sender() returns a pointer to the object that emitted the signal. In your case (if I understood correctly) it's one of the widget in the QTabWidget.

    You can cast (using qobject_cast) that pointer to QWidget * and then get the tab index using indexOf and update the correct tab text with it.



  • thx
    [quote author="SGaist" date="1377806355"]sender() returns a pointer to the object that emitted the signal. In your case (if I understood correctly) it's one of the widget in the QTabWidget. [/quote]

    but the QObject that emitted the signal is a QWebView which is contain in a QTabWidget can i have a access to the tab with a pointer of QWebView
    fanction like "findChild" but in this one i have to find the parent
    thx again


  • Lifetime Qt Champion

    You don't need that, since you call addTab with a QWebView you can retrieve the corresponding index with the pointer given by sender() once you casted it.



  • QObject * QObject::sender() const [protected] where do i declare this methode, in which slot.
    i have also this slot
    @void mainWind::addtap()
    {
    QWidget *tap=new QWidget();
    QVBoxLayout *layout = new QVBoxLayout();
    QWebView *webpage=new QWebView();
    pageWeb->settings()->setIconDatabasePath("a");
    layout->addWidget(webpage);
    tap->setLayout(layout);
    webpage->load(QUrl(""));
    ui->tabWidget->insertTab(ui->tabWidget->indexOf(ui->newTap),tap,"");
    ui->tabWidget->setCurrentWidget(tap);
    changetap(ui->tabWidget->indexOf(tap));

    }@


  • Lifetime Qt Champion

    You don't declare that method. You call it at the start of the slot



  • see the doc of "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender .



  • @void mainWind::titre(const QString & titre)
    {
    QWidget *a= qobject_cast<QWidget *>(sender());
    QString titr(titre);
    if(titre.size()>40)
    {
    titr=titre.left(40)+"....";
    }
    ui->tabWidget->setTabText(ui->tabWidget->indexOf(a)),titr);
    }@
    is this correct


Log in to reply
 

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