Important: Please read the Qt Code of Conduct -

[Solved] [Not possible] How can I connect things from other Header Files which are "not declared in this Scope"?

  • Sorry. Totally forgot to update the Status to solved.


    I am trying to cleanup my "mainwindow.cpp" and "mainwindow.h".

    I have moved some Stuff to "toollist.cpp" and "toollist.h". The following QAction is still in "mainwindow.cpp":

    //Actions fuer Menue Tools...
    toollistWindowsTaschenrechner = new QAction(tr("Windows Taschenrechner"), this);
    connect(toollistWindowsTaschenrechner,SIGNAL(triggered()), this, SLOT(WindowsTaschenrechner()));

    During Building+Compiling i get the Error:
    "toollistWindowsTaschenrechner' was not declared in this scope"

    My toollist.h contains:
    explicit Toollist(QObject *parent = 0);
    //Actions fuer Menue Tools...
    QAction *toollistWindowsTaschenrechner;

    The toollist.cpp has the following code:
    void Toollist::WindowsTaschenrechner()

    Maybe i am too dumb but i dont get it: Why does it dont work anymore? If the above Stuff is in the mainwindow.* Files, it compiles without any Error.

    And yes, #include "toollist.h" is in mainwindow.cpp.


  • You need to create an object of Toollist before you can connect any signal/slot connection. I guess that is the difference you had just 1 class before and now you have 2, so you need an objects for both classes to connect them.
    But I don't know to be honest your code snippets look a little weird to me :D

  • [quote author="Xander84" date="1395928169"]You need to create an object of Toollist before you can connect any signal/slot connection. I guess that is the difference you had just 1 class before and now you have 2, so you need an objects for both classes to connect them.
    But I don't know to be honest your code snippets look a little weird to me :D[/quote]

    Ok thanks for the Hint. Going back to the Tutorials...

  • Evening.

    After I took another Look at it, i finally managed to get the Stuff working:

    A.) I created a new Header+CPP File and moved all the Stuuf over there
    B.) Created a class for the Actions and the Menu
    C.) Create and Call the Object of the Class from the "mainwindow.cpp"

    One Error gone. So far so good. But:
    After I have moved all the things to a seperate Header+Class File to get a cleaner mainwindow.cpp, the Compiler now tells me

    "Invalid use of incomplete type QAction"

    sigh -.- Well one thing after another.

  • Lifetime Qt Champion


    You are missing

    @#include <QAction>@

    In the cpp file where you are using it

  • ROFL Oh my yes you are right. Such a dumb mistake. I moved all other #include Files over from mainwindow.cpp. But I forgot that one somehow.

    Anyway: I am sure I had remembered this in the next few Minutes but thanks for saving me some time :)

    If nothing else related to this comes up, i will finally declare this Topic as Solved.


    [quote author="SGaist" date="1405368983"]Hi,

    You are missing

    @#include <QAction>@

    In the cpp file where you are using it[/quote]

  • Oh fantastic: Now i get

    "undefined Reference to actionsCreate::createActions" and
    "undefined Reference to menuCreate::createMenu in mainwindow.cpp"

    But the classes and Functions are still in menueactions.h and menueactions.cpp

    In mainwindow.cpp i call:
    //For the Actions...
    actionsCreate newActions;

    //For the Menu...
    menuCreate newMainMenu;

    actionsCreate and menuCreate are the Names of the Classes. createActions and createMenu are the Functions within the classes.

    I have changed nothing. Did a clean -> qmake -> rebuild, restarted Creator but nothing. Strange. It just stopped working.

    Has anyone an Idea?

  • Lifetime Qt Champion

    Aren't you moving a bit too much stuff around ? Why a new class for the actions and the menu bar ?

  • I want the mainwindow.cpp as clean as possible. I want to learn to write good and clean code even if i will never make money of it. Its a personal thing.

    But I needed to create Objects of/for the Functions to be able to have a working Menu and creating those classes works for me.

    Is there a better Solution?

  • Lifetime Qt Champion

    Then have a look at Qt's example/demos coding style. They are pretty clean.

  • Evening.

    I found a little time and got things to work. Even all Segmentation Faults are gone. But as usual i ran into another Problem:

    Having my Menu and Actions combined into MenueActions.cpp, I now call the Menu and Action Creation like this:

    In MainWindow.cpp:
    //Creating an Object of MenueActions...
    MenueActions mainWindowMainMenu;
    //Using the Object and call MenueActions::createMenu()...

    The Code in MenueActions.cpp -> createMenu() is:
    void MenueActions::createMenu()
    mainWindowMainMenuBar = new QMenuBar(0);

    Datei = new QMenu(0);
    dateiNeu = new QAction(tr("Neue leere Datei erzeugen"), this);
    connect(dateiNeu,SIGNAL(triggered()), this, SLOT(Neu()));
    dateiNeu->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
    dateiNeu->setWhatsThis(tr("Erzeugt eine neue leere Text Datei"));
    dateiNeu->setStatusTip(tr("Erzeugt eine neue leere Text Datei"));
    dateiNeu->setToolTip(tr("Erzeugt eine neue leere Text Datei"));

    [Cutted out the "same code" for other Menu Entrys...]



    No Errors but the Menu dont show up in the MainWindow of the Application. Using "show();" just execute the Menu (in a second Window). So i disabled that again.

    I am using a Layout. The current Code within mainwindow.cpp is:

    mainWindowWidget = new QWidget(this);

    //Main Layout!
    qGridLayout = new QGridLayout;

    LinkListe = new QTextEdit;

    //Einfacher Texteditor (Zu Beginn nur ein simpler Dateibetrachter!)
    TextEditor = new QTextEdit;
    OpenAntrag = new QTextEdit;
    statusBar = new QStatusBar;
    statusBar->showMessage(tr("I am the Statusbar"), 90000);

    statusBarLayout = new QHBoxLayout;

    layoutLinkListe = new QVBoxLayout;

    layoutTextEditor = new QVBoxLayout;
    layoutOpenAntrag = new QVBoxLayout;
    qGridLayout->addLayout(layoutLinkListe, 0,0);
    qGridLayout->addLayout(layoutTextEditor, 0,1);
    qGridLayout->addLayout(layoutOpenAntrag, 0,2);
    qGridLayout->addLayout(statusBarLayout, 1,0);
    MenueActions mainWindowMainMenu;


    I have found a dozen of Tips for Qt 4.x - but that haven't helped me in anyway: Functions like "exec()" or "run()" are not available under Qt 5.3.1

    Should i create an extra Class and put the Layout and Menu Creation into one?

    Thanks and good night :)

  • Lifetime Qt Champion

    There's no need for mainWindowMainMenuBar, QMainWindow already has a menu bar that you can retrieve with menuBar()

    As for why it doesn't show, you don't put it in any layout nor are you calling show on it.

Log in to reply