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. Help global variable
Qt 6.11 is out! See what's new in the release blog

Help global variable

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 745 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.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on last edited by
    #1

    I believe this is a matter purely C + +, since my experience with C + + is small.

    I have the following code

    @
    /*

    • dynamicuitest.hpp
    • This file is part of DynamicUi.so
    • Copyright (C) 2014 - Matheus Saraiva
    • DynamicUi.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • DynamicUi.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with DynamicUi.so. If not, see http://www.gnu.org/licenses/.
      */

    #ifndef DYNAMICUITEST_H

    define DYNAMICUITEST_H

    #include <QString>
    #include <QtTest>
    #include "dynamicui.hpp"

    class DynamicUiTest : public QObject
    {
    Q_OBJECT

    public:
    DynamicUiTest();

    private Q_SLOTS:
    void testCreateUi();
    void testCreateUi_data();
    };
    #endif
    @

    @
    /*

    • dynamicuitest.cpp
    • This file is part of DynamicUi.so
    • Copyright (C) 2014 - Matheus Saraiva
    • DynamicUi.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • DynamicUi.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with DynamicUi.so. If not, see http://www.gnu.org/licenses/.
      */

    #include "dynamicuitest.hpp"

    QWidget *widtest; // Pointer for object recepted from DynamicUi

    DynamicUiTest::DynamicUiTest()
    {
    }

    void DynamicUiTest::testCreateUi()
    {
    QFETCH(QString, file_ui);
    QFETCH(bool, widget);
    DynamicUi lui(file_ui); // DynamicUi object
    widtest = lui.createUi();

    QCOMPARE(widtest->isWidgetType(), widget); // Compare DynamicUi::createUi() return with QWidget type
    }

    void DynamicUiTest::testCreateUi_data()
    {
    QTest::addColumn<QString>("file_ui");
    QTest::addColumn<bool>("widget");

    QTest::newRow("Valid File") << "test.ui" << true; // Valid File
    QTest::newRow("Inexistent File") << "inexistent.ui" << true; // Inexistent File
    QTest::newRow("Invalid File") << "test_text.ui" << true; // Invalid File
    QTest::newRow("Empty Ui File") << "testempty.ui" << true; // Empty File
    delete widtest;
    }
    @

    If I move "QWidget * widtest" of dynamicuitest.cpp to dynamicuitest.hpp. Becoming a global variable,

    @
    /*

    • dynamicuitest.hpp
    • This file is part of DynamicUi.so
    • Copyright (C) 2014 - Matheus Saraiva
    • DynamicUi.so is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License as published by
    • the Free Software Foundation; either version 2 of the License, or
    • (at your option) any later version.
    • DynamicUi.so is distributed in the hope that it will be useful,
    • but WITHOUT ANY WARRANTY; without even the implied warranty of
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    • GNU General Public License for more details.
    • You should have received a copy of the GNU General Public License
    • along with DynamicUi.so. If not, see http://www.gnu.org/licenses/.
      */

    #ifndef DYNAMICUITEST_H

    define DYNAMICUITEST_H

    #include <QString>
    #include <QtTest>
    #include "dynamicui.hpp"

    class DynamicUiTest : public QObject
    {
    Q_OBJECT

    public:
    DynamicUiTest();

    private:
    QWidget *widtest;

    private Q_SLOTS:
    void testCreateUi();
    void testCreateUi_data();
    };
    #endif
    @

    fail at runtime.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Exotic_Devel
      wrote on last edited by
      #2

      Ok, now I understand the difference Global Variable and Variable of Instance.

      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