Scope question 'Config Dialog Example'
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.
@class ConfigPage : public QWidget
ConfigPage(QWidget *parent = 0);
and tried to acces in pages.cpp
qDebug() << ConfigPage::myPort->isOpen();
@/pages.h:26: error: invalid use of non-static data member 'ConfigPage::myPort'
Doing the following works, but is this the correct ay ?
// both in pages.cpp
qDebug() << myPort->isOpen();
Jeroentjehome last edited by
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 QueryPage(myPort)); // Let's assume you start remodeling this one!
// Constructor of QueryPage:
QueryPage::QueryPage(QSerialPort *myPort, 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.
@ pagesWidget->addWidget(new AnalysePage);
@class TestPage : public QWidget
TestPage(QSerialPort *myPort, QWidget *parent = 0);
@TestPage::TestPage(QSerialPort *myPort, QWidget *parent)
@mainwindow.cpp:46: error: 'myPort' was not declared in this scope
Do you see the error and/or the solution ?
Jeroentjehome last edited by
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
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:
the pre/ postfix notification is a good idea. The name is now:
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 ?
Do i think in the right direction now ?