Scope question 'Config Dialog Example'



  • Hello,

    i started a new project on base of the "Config Dialog Example" from Qt 5.2.1
    and modified for my needs.
    In an added slot of class ConfigurationPage in pages.cpp i want to create
    a QSerialPort in the usual way using QSerialPortInfo.
    How and in which location i must declare the QSerialPort *myPort pointer
    if i want to create and open it in class ConfigurationPage but access it
    for further use in class QueryPage also in pages.cpp file ?
    I hope the question is more or less clear.

    I tried:
    in pages.h
    @class ConfigPage : public QWidget
    {
    Q_OBJECT
    public:
    ConfigPage(QWidget *parent = 0);
    ~ConfigPage();

    QSerialPort *myPort;@
    

    and tried to acces in pages.cpp
    @void TestPage::button_ONE()
    {

    qDebug() << ConfigPage::myPort->isOpen();
    

    @

    i get:
    @/pages.h:26: error: invalid use of non-static data member 'ConfigPage::myPort'
    QSerialPort *myPort;
    ^@



  • Doing the following works, but is this the correct ay ?
    // both in pages.cpp

    @#include "pages.h"
    #include "mainwindow.h"

    QSerialPort *myPort;

    ConfigPage::ConfigPage(QWidget *parent)
    : QWidget(parent)
    {
    ...@

    @void TestPage::button_ONE()
    {
    qDebug() << myPort->isOpen();
    }@



  • Hi,
    You're trouble by the C++ capabilities ;-) What you did is to make the myPort pointer a GLOBAL variable. This is highly unwanted if you want to stay within the 'Object Orientated' world.
    The second thing is that you mix two (or more) classes in a single header/source files? Also not really nice.
    It is better to keep them separated so you don't mix up variables or unwanted methode calls etc.
    Please first tell what you want to do? Not only the problem you encounter. We might give some insight how to setup the program and then help you with individual problems.
    I read the ConfigDialog example and what you probably want is to add the myPort variable to the constructor of your page. This way it is available in your buttonclick for that page.
    So something like this:
    @
    pagesWidget = new QStackedWidget;
    pagesWidget->addWidget(new ConfigurationPage);
    pagesWidget->addWidget(new UpdatePage);
    pagesWidget->addWidget(new QueryPage(myPort)); // Let's assume you start remodeling this one!

    // Constructor of QueryPage:
    QueryPage::QueryPage(QSerialPort *myPort, QWidget Parent) :
    QWidget(parent),
    m_MyPort(myPort) // Take the pointer and store in a member variable for later reference!
    @



  • bq. What you did is to make the myPort pointer a GLOBAL variable. This is highly unwanted

    agree, that's why ask if this might be correct.

    bq. The second thing is that you mix two (or more) classes in a single header/source files? Also not really nice.

    I learned this from an original Qt example. Usual i accept original examples as professional solutions and tutorial.

    bq. Please first tell what you want to do?

    I want to create a SerialportSniffer to observe whats going on between a device and the host PCs port.
    I need 2 ports for reading Rx and Tx lines. In configuration i want to setup this ports and observing analyzing and testing protocol in other classes as in example class UpdatePage and QueryPage.

    I think you do not want me to send the entire project, right ?

    Now study your code suggestion first and try to understand. thank you !



  • OK, i splitted all header and definition files as you recommended.
    Much more clear now.

    Then i tried the rest.
    mainwindow.cpp
    @ pagesWidget->addWidget(new AnalysePage);
    pagesWidget->addWidget(new GpibPage);
    pagesWidget->addWidget(new TestPage(myPort));
    @
    testpage.h
    @class TestPage : public QWidget
    {
    Q_OBJECT

    public:
    TestPage(QSerialPort *myPort, QWidget *parent = 0);
    ~TestPage();@
    testpage.cpp
    @TestPage::TestPage(QSerialPort *myPort, QWidget *parent)
    : QWidget(parent),
    m_MyPort(myPort)
    {
    ...@
    i get
    @mainwindow.cpp:46: error: 'myPort' was not declared in this scope
    pagesWidget->addWidget(new TestPage(myPort));
    ^
    @
    Do you see the error and/or the solution ?



  • Hi,
    Because you make the widgets in MainWindow, the myPort variable needs to be within that scope. I would recommend to make it a private variable of the MainWindow class. Then when you do the
    @
    pagesWidget->addWidget(new TestPage(myPort));
    @
    It will give the widget TestPage the pointer to your myPort variable and thus given it full control of the Port. When the dialog is closed the myPort variable is setup and still active.
    BTW as a coding standard issue I always use postfixes to indicate what type of variable it is your working with. So quint32 gets a _u32 added showing it to be a unsigned 32 bits variable. For pointers and references I normally use p as postfix. Next to that I use the m prefix when it is a member variable of the class. This to make a difference between class members and function scope variables.
    So the function looks like:
    @
    pagesWidget->addWidget(new TestPage(m_myPort_p));
    @



  • the pre/ postfix notification is a good idea. The name is now:
    m_MasterPort_p

    thanks for you patience, I know its an elementary c++ class question and not a Qt one, i already study tutorials.

    bq. ... make it a private variable of the MainWindow class

    This is OK so far, but i select the port in configpage and want to use it in testpage. So i think i must do the same for configpage as for testpage constructor, right ?

    @pagesWidget->addWidget(new ConfigPage(m_MasterPort_p));
    pagesWidget->addWidget(new TestPage(m_MasterPort_p));@

    Do i think in the right direction now ?


Log in to reply
 

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