Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Slots and signals from different classes

Slots and signals from different classes

Scheduled Pinned Locked Moved General and Desktop
42 Posts 5 Posters 15.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    Hello everyone,

    I have two different classes that I am trying to connect. 'browseTab' has a button in it that, the slot to execute when that button is clicked is in the 'mainView' class. My program compiles, but the signal from the browseTab button does not seem to be connected to the mainView slot. How can I call a signal in one class and have the slot be in another class? I have google around and I saw a bunch of stuff about this, but it wasn't very helpful.

    code:

    browseTab.h:
    @
    #ifndef BROWSETAB_H
    #define BROWSETAB_H

    #include <QWidget>
    #include <QLineEdit>
    #include <QWebView>
    #include <QToolBar>

    #include "mainview.h"

    class browseTab : public QWidget
    {
    Q_OBJECT
    public:
    explicit browseTab(QWidget *parent = 0);

    signals:

    protected slots:
    void makeSearchURL();
    void makeSearch();
    void changeText();
    void changeTabText();

    private:
    QWebView *webView;
    QToolBar *navBar;
    QLineEdit *URLbar;
    QLineEdit *SearchBar;
    QToolButton *addTabButton;

    };

    #endif // BROWSETAB_H
    @

    browseTab.cpp:
    @
    addTabButton = new QToolButton;
    addTabButton->setText("New Tab");
    connect(addTabButton, SIGNAL(clicked()), SLOT(newTab()));
    @

    mainView.h:
    @
    #ifndef MAINVIEW_H
    #define MAINVIEW_H

    #include <QMainWindow>
    #include <QToolButton>
    #include <QMessageBox>

    #include "apptab.h"
    #include "browsetab.h"
    #include "novablastreceiver.h"
    #include "novablastsender.h"

    class mainView : public QMainWindow
    {
    Q_OBJECT

    public:
    mainView(QWidget *parent = 0);
    ~mainView();

    signals:

    public slots:
    void newTab();

    protected slots:
    void newWindow();
    //void newTab();
    void closeTab(int index);
    void newAppTab();

    void setFullscreen();
    void setMaximized();
    void setWindowed();
    
    void startNBSender();
    void startNBReceiver();
    

    private:
    QTabWidget *tabWidget;
    QToolButton *newTabButton;
    QToolButton *newAppTabButton;
    QToolButton *menuButton;
    QAction *NBSenderAction;
    QAction *NBReceiverAction;
    QAction *FullscreenAction;
    QAction *MaximizedAction;
    QAction *WindowedAction;
    QAction *NewWindowAction;
    QAction *NewTabAction;
    QAction *AppsAction;
    QAction *BookmarksAction;
    QAction *HistoryAction;
    appTab *Apps;
    QMenu *menuButtonMenu;
    QMenu *toolMenu;
    QMenu *viewMenu;
    QWidget *mainWidget;
    QDialog *receiverDialog;
    QDialog *senderDialog;
    Receiver *newNBReceiver;
    Sender *newNBSender;

    };

    #endif // MAINVIEW_H
    @

    mainView.cpp:
    @
    void mainView::newTab()
    {
    tabWidget->addTab(new browseTab, tr("Search"));
    }
    @

    example code would be great!

    thanks!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hareen Laks
      wrote on last edited by
      #2

      You have to create a pointer to object of main view to connect the newTab(); slot. If not how to identify newTab() method of which class or which object.

      @connect(addTabButton, SIGNAL(clicked()),mainView_object_pointer, SLOT(newTab()));@

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicky j
        wrote on last edited by
        #3

        Thanks! how do I create a pointer to it?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hareen Laks
          wrote on last edited by
          #4

          In browseTab.h under the

          @private:
          mainview *mainView_object_pointer;
          @

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nicky j
            wrote on last edited by
            #5

            oh okay. Thanks a lot!

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nicky j
              wrote on last edited by
              #6

              ok so when I do that, It says 'unknown type name:"mainView"'. What am I doing wrong?

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nicky j
                wrote on last edited by
                #7

                Okay so ive gotten my program to compile. but when I click the addTabButton, it does nothing. The compiler outputs: "QObject::connect: Cannot connect QToolButton::clicked() to (null)::newTab()"

                here is the code:

                browseTab.h:
                @#ifndef BROWSETAB_H
                #define BROWSETAB_H

                #include <QWidget>
                #include <QPointer>
                #include <QLineEdit>
                #include <QWebView>
                #include <QToolBar>

                #include "mainview.h"
                #include <mainview.h>

                class mainView;

                class browseTab : public QWidget
                {
                Q_OBJECT
                public:
                explicit browseTab(QWidget *parent = 0);

                signals:

                protected slots:
                void makeSearchURL();
                void makeSearch();
                void changeText();
                void changeTabText();

                private:
                QWebView *webView;
                QToolBar *navBar;
                QLineEdit *URLbar;
                QLineEdit *SearchBar;
                QToolButton *addTabButton;
                mainView *mainView_object_pointer;

                };

                #endif // BROWSETAB_H
                @

                browsetab.cpp:

                @#include "browsetab.h"
                #include "mainview.h"

                #include <QtWidgets>

                browseTab::browseTab(QWidget *parent) :
                QWidget(parent),
                mainView_object_pointer()
                {
                addTabButton = new QToolButton;
                addTabButton->setText("+");
                connect(addTabButton, SIGNAL(clicked()), mainView_object_pointer, SLOT(newTab()));
                }
                @

                mainview.h:

                @#ifndef MAINVIEW_H
                #define MAINVIEW_H

                #include <QMainWindow>
                #include <QToolButton>
                #include <QMessageBox>

                #include "apptab.h"
                #include "browsetab.h"
                #include "novablastreceiver.h"
                #include "novablastsender.h"

                class mainView : public QMainWindow
                {
                Q_OBJECT

                public:
                mainView(QWidget *parent = 0);
                ~mainView();

                signals:

                public slots:
                void newTab();

                protected slots:
                void newWindow();
                void closeTab(int index);
                void newAppTab();

                void setFullscreen();
                void setMaximized();
                void setWindowed();
                
                void startNBSender();
                void startNBReceiver();
                

                private:
                QTabWidget *tabWidget;
                QToolButton *newTabButton;
                QToolButton *newAppTabButton;
                QToolButton *menuButton;
                QAction *NBSenderAction;
                QAction *NBReceiverAction;
                QAction *FullscreenAction;
                QAction *MaximizedAction;
                QAction *WindowedAction;
                QAction *NewWindowAction;
                QAction *NewTabAction;
                QAction *AppsAction;
                QAction *BookmarksAction;
                QAction *HistoryAction;
                appTab *Apps;
                QMenu *menuButtonMenu;
                QMenu *toolMenu;
                QMenu *viewMenu;
                QWidget *mainWidget;
                QDialog *receiverDialog;
                QDialog *senderDialog;
                Receiver *newNBReceiver;
                Sender *newNBSender;

                };

                #endif // MAINVIEW_H
                @

                mainview.cpp:

                @#include <QtWidgets>

                #include "mainview.h"

                mainView::mainView(QWidget *parent)
                : QMainWindow(parent)
                {
                void mainView::newTab()
                {
                tabWidget->addTab(new browseTab, tr("Search"));
                }
                }
                @

                How can I make the addTabButton actually call the newTab() slot in main view.cpp

                It doesn't seem to work

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kpks
                  wrote on last edited by
                  #8

                  i am pretty new too, but, I don't see where you assign your pointer to the mainview.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    eyenov
                    wrote on last edited by
                    #9

                    if you are doing the "connect" call inside one of your mainView's functions, you can call it like this:

                    @connect(addTabButton, SIGNAL(clicked()), this, SLOT(newTab()));@

                    It should work...

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nicky j
                      wrote on last edited by
                      #10

                      The connect is being called from browseTab, and the slot I want executed is in mainView. Help would be greatly appreciated!

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        eyenov
                        wrote on last edited by
                        #11

                        Change your browsetab.cpp to:

                        @#include "browsetab.h"
                        #include "mainview.h"

                        #include <QtWidgets>

                        browseTab::browseTab(QWidget *parent) :
                        QWidget(parent),
                        mainView_object_pointer(parent->parent())
                        {
                        addTabButton = new QToolButton;
                        addTabButton->setText("+");
                        connect(addTabButton, SIGNAL(clicked()), mainView_object_pointer, SLOT(newTab()));
                        }@

                        And when creating the tab, change to this:

                        @
                        #include <QtWidgets>

                        #include "mainview.h"

                        mainView::mainView(QWidget *parent)
                        : QMainWindow(parent)
                        {

                        //you should create tabWidget passing this as a parameter
                        tabWidget = new QTabWidget(this);

                        }

                        void mainView::newTab()
                        {
                        tabWidget->addTab(new browseTab(tabWidget), tr("Search"));
                        }
                        @

                        dont forget that when creating your tabWidget object (in mainView constructor?)you need to pass 'this' pointer for the mainView in the constructor,

                        hope this helps...

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nicky j
                          wrote on last edited by
                          #12

                          Okay, although it compiles and runs, the button in the browseTab still won't create a new tab.

                          It gives me this error: "QObject::connect: Cannot connect QToolButton::clicked() to (null)::newTab()"

                          How do I fix this?
                          remember the addTab button is in browseTab, and newTab() is in mainView

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            eyenov
                            wrote on last edited by
                            #13

                            Are you sure you changed the way your QTabWidget is being created to:
                            @tabWidget = new QTabWidget(this);@

                            Just to give you a brief explanation:

                            1. you need to create your "tabWidget" with "this" (mainView) as a parent so you can access it on "browseTab" constructor
                            2. in "addTab" you need to create your "browseTab" object with "tabWidget" as a parent so you can access it on "browseTab" constructor
                            3. you need to set "mainView_object_pointer" to "browseTab" parent's parent
                            4. you need to connect "clicked" signal from "addTabButton" to slot "newTab" on "mainView_object_pointer"

                            if you are following this, then your "mainView_object_pointer" can't be null as you are saying in the error message.

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              nicky j
                              wrote on last edited by
                              #14

                              Ok so I think my problem is number 2. How can I make browseTab with tabWidget as a parent? Thanks for the help!

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                eyenov
                                wrote on last edited by
                                #15

                                In your "newTab" function create your new "browseTab" object with "tabWidget" as a parameter.

                                @void mainView::newTab()
                                {
                                tabWidget->addTab(new browseTab(tabWidget), tr("Search"));
                                }@

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  nicky j
                                  wrote on last edited by
                                  #16

                                  Okay Ive tried that and now I get an error:

                                  "error: cannot initialize a member subobject of type 'mainView *' with an rvalue of type 'QObject *'
                                  mainView_object_pointer(parent->parent())"

                                  1 Reply Last reply
                                  0
                                  • E Offline
                                    E Offline
                                    eyenov
                                    wrote on last edited by
                                    #17

                                    In browseTab.h where you have:
                                    @mainview *mainView_object_pointer;@

                                    change to:
                                    @QObject *mainView_object_pointer;@

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      nicky j
                                      wrote on last edited by
                                      #18

                                      hmmmm, just did that. The program appears to compile just fine and starts to run (the icon on the dock starts to bounce), it crashes and says "the program has unexpectedly finished" which is obviously not very helpful. I am running Qt Creator on OS X 10.9 if that matters. What do I do?

                                      1 Reply Last reply
                                      0
                                      • E Offline
                                        E Offline
                                        eyenov
                                        wrote on last edited by
                                        #19

                                        You can send me the code if you want to for me to check it out, Will it run on windows or does it have any macosx dependencies?

                                        1 Reply Last reply
                                        0
                                        • N Offline
                                          N Offline
                                          nicky j
                                          wrote on last edited by
                                          #20

                                          browseTab.h:
                                          @#ifndef BROWSETAB_H
                                          #define BROWSETAB_H

                                          #include <QWidget>
                                          #include <QPointer>
                                          #include <QLineEdit>
                                          #include <QWebView>
                                          #include <QToolBar>
                                          #include <QObject>

                                          #include "mainview.h"
                                          #include <mainview.h>

                                          class mainView;

                                          class browseTab : public QWidget
                                          {
                                          Q_OBJECT
                                          public:
                                          explicit browseTab(QWidget *parent = 0);

                                          signals:

                                          protected slots:
                                          //void makeSearchURL();
                                          void makeSearch();
                                          void changeText();
                                          void changeTabText();

                                          private:
                                          QWebView *webView;
                                          QToolBar *navBar;
                                          //QLineEdit *URLbar;
                                          QLineEdit *SearchBar;
                                          QToolButton *addTabButton;
                                          QObject *mainView_object_pointer;

                                          };

                                          #endif // BROWSETAB_H
                                          @

                                          browseTab.cpp:
                                          @#include "browsetab.h"
                                          #include "mainview.h"

                                          #include <QtWidgets>

                                          browseTab::browseTab(QWidget *parent) :
                                          QWidget(parent),
                                          mainView_object_pointer(parent->parent())
                                          {
                                          QString url = "http://www.google.com";

                                          webView = new QWebView;
                                          webView->load(url);
                                          connect(webView, SIGNAL(loadFinished(bool)), SLOT(changeText()));
                                          connect(webView, SIGNAL(loadFinished(bool)), SLOT(changeTabText()));
                                          

                                          /*
                                          URLbar = new QLineEdit(this);
                                          URLbar->setStyleSheet(
                                          "background-color:white;"
                                          "color:black;"
                                          "border:1px solid rgba(47, 74, 135);"
                                          );
                                          connect(URLbar, SIGNAL(returnPressed()), SLOT(makeSearchURL()));
                                          */
                                          SearchBar = new QLineEdit(this);
                                          SearchBar->setStyleSheet(
                                          "background-color:white;"
                                          "color:black;"
                                          "border:1px solid black;"
                                          "margin-top:0px;"
                                          "margin-bottom:0px;"
                                          );
                                          connect(SearchBar, SIGNAL(returnPressed()), SLOT(makeSearch()));

                                          addTabButton = new QToolButton;
                                          addTabButton->setText("+");
                                          connect(addTabButton, SIGNAL(clicked()), mainView_object_pointer, SLOT(newTab()));
                                          
                                          QToolBar *navBar = new QToolBar;
                                          navBar->setStyleSheet(
                                                      "height:20px;"
                                                      );
                                          navBar->addAction(webView->pageAction(QWebPage::Back));
                                          navBar->addAction(webView->pageAction(QWebPage::Forward));
                                          navBar->addAction(webView->pageAction(QWebPage::Reload));
                                          navBar->addAction(webView->pageAction(QWebPage::Stop));
                                          //navBar->addWidget(URLbar);
                                          navBar->addWidget(SearchBar);
                                          navBar->addWidget(addTabButton);
                                          
                                          QVBoxLayout *browseTabLayout = new QVBoxLayout;
                                          browseTabLayout->addWidget(navBar);
                                          browseTabLayout->addWidget(webView);
                                          setLayout(browseTabLayout);
                                          

                                          }

                                          //browseTab-- SLOTS

                                          void browseTab::changeText()
                                          {
                                          SearchBar->setText(webView->url().toString());
                                          }

                                          void browseTab::changeTabText()
                                          {

                                          }
                                          /*
                                          void browseTab::makeSearchURL()
                                          {
                                          webView->setUrl(URLbar->text());
                                          }
                                          */
                                          void browseTab::makeSearch()
                                          {
                                          QString input = SearchBar->text();
                                          QUrl query = SearchBar->text();
                                          QString google = "/google";
                                          QString newsfly = "/newsfly";

                                          if(input.startsWith("http://"))
                                          {
                                              webView->setUrl(query);
                                          }
                                          
                                          else
                                          {
                                              if(input.startsWith("%"))
                                              {
                                                  if(input == google)
                                                  {
                                                      webView->setUrl(QUrl(QString("http://www.google.com/")));
                                                  }
                                          
                                                  if(input == newsfly)
                                                  {
                                                      webView->setUrl(QUrl(QString("http://www.newsfly.x10.mx/")));
                                                  }
                                              }
                                          
                                              else
                                              {
                                                  webView->setUrl(QUrl(QString("http://www.google.com/search?fs&q=")+(SearchBar-&gt;text())));
                                              }
                                          }
                                          

                                          }

                                          @

                                          mainview.h:
                                          @#ifndef MAINVIEW_H
                                          #define MAINVIEW_H

                                          #include <QMainWindow>
                                          #include <QToolButton>
                                          #include <QMessageBox>

                                          #include "apptab.h"
                                          #include "browsetab.h"
                                          //#include "novablastreceiver.h"
                                          //#include "novablastsender.h"

                                          class mainView : public QMainWindow
                                          {
                                          Q_OBJECT

                                          public:
                                          mainView(QWidget *parent = 0);
                                          ~mainView();

                                          signals:

                                          public slots:
                                          void newTab();

                                          protected slots:
                                          void newWindow();
                                          void closeTab(int index);
                                          void newAppTab();
                                          void setFullscreen();
                                          void setMaximized();
                                          void setWindowed();
                                          //void startNBSender();
                                          //void startNBReceiver();

                                          private:
                                          QTabWidget *tabWidget;
                                          QToolButton *newTabButton;
                                          QToolButton *newAppTabButton;
                                          QToolButton *menuButton;
                                          QAction *NBSenderAction;
                                          QAction *NBReceiverAction;
                                          QAction *FullscreenAction;
                                          QAction *MaximizedAction;
                                          QAction *WindowedAction;
                                          QAction *NewWindowAction;
                                          QAction *NewTabAction;
                                          QAction *AppsAction;
                                          QAction *BookmarksAction;
                                          QAction *HistoryAction;
                                          appTab *Apps;
                                          QMenu *menuButtonMenu;
                                          QMenu *toolMenu;
                                          QMenu *viewMenu;
                                          QWidget *mainWidget;
                                          QDialog *receiverDialog;
                                          QDialog *senderDialog;
                                          //Receiver *newNBReceiver;
                                          //Sender *newNBSender;

                                          };

                                          #endif // MAINVIEW_H
                                          @

                                          fair warning: there is a lot of code to sift through, but much of it is commented out, and you appear to be a great programmer so you shouldn't have any trouble. Thanks for your time!

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved