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. Qt help
Forum Update on Monday, May 27th 2025

Qt help

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 678 Views
  • 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.
  • C Offline
    C Offline
    CharlesHuff
    wrote on last edited by
    #1

    I have a lib called sFoundation that is used to drive a stepper motor.
    This Library has classes for SysManager, Ports, and Nodes. Only one instance of
    SysManager is needed per application. I am using one motor so there is only
    one Port and one Node on that port. I have compiled and linked my Qt 6 widget
    app and the following code added to MainWindow.cpp will find, open then
    close the correct port.

    This is great, however the sFoundation lib object should not have to be created and destroyed
    for each call to its members.

    If I open, use, then close the Port all within MainWindow creator it works.
    Ideally I would do this all except the myMgr->PortsClose() leaving the SysManager class
    active so its member functions could be altered/read in the various pushbutton slot
    functions, but qDebug() is showing me it is crashing.

    '''
    #include "path to lib file headers"
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    this->setWindowFlags(this->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
    ui->setupUi(this);
    ui->horizontalSlider->setRange(0,100799);
    ui->verticalSlider->setRange(1,1000);
    ui->Engaged->setChecked(false);
    ui->ActualPosn->setText("0");
    ui->CommandedPosn->setText("0");
    ui->lineEdit_speed->setText("1");
    std::vectorstd::string comHubPorts;
    SysManager *myMgr;
    IPort *iPort;
    INode *theNode;
    size_t portCount = 0;
    QString myStr;
    QString myStr2;
    //QString myStr3;
    bool myEStop;
    char alertList[256];
    double timeout;
    double speed;
    double commPosn;
    myMgr = SysManager::Instance();
    myMgr->SysManager::FindComHubPorts(comHubPorts);
    myStr = QString::number(comHubPorts.size());
    myMgr->ComHubPort(0, comHubPorts[0].c_str());
    iPort = &myMgr->Ports(0);
    myMgr->PortsOpen(1);
    qInfo() << "Found:" << myStr;
    myStr2 = comHubPorts[0].c_str();
    qInfo() << "Device: " << myStr2;
    qDebug() << "NetNumber: " << QString::number(iPort->NetNumber());
    qDebug() << "OpenState: " << QString::number(iPort->OpenState());
    if (iPort->NodeCount() == 0) {
    qDebug() << "zero! No Nodes open error...";
    } else {
    qDebug() << "Node: " << QString::number(iPort->NodeCount());
    }
    //qDebug() << "Closing Ports now";

    myMgr->PortsClose();  //this works here
    
    
    qDebug() << "MainWindow is done";
    

    }

    void MainWindow::on_pushButton_EndPgm_clicked()
    {
    //myMgr->PortsClose(); //this does not work here?
    qApp->quit();
    }
    '''
    any tips or help are greatly appreciated.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Is myMgr a class member ?
      If so, you shadowed it in your constructor.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CharlesHuff
        wrote on last edited by
        #3

        I think SysManager is the class from sFoundation. myMgr is the instance of the class. What is "shadowed"? How may I use the class without it being shadowed assuming this is wrong?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You did not answer my question: is myMgr a class member of MainWindow ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CharlesHuff
            wrote on last edited by
            #5

            Sorry, I don't think it could be...

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Do you realize that this is a yes/no type of question ?

              If not, you should really take the time to learn the C++ basics.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                CharlesHuff
                wrote on last edited by
                #7

                Thank you. I realize my questions are dumb.
                I did a tutorial on classes. I can write basic c++ code outside of Qt that runs ok. No other person/tutorial has used the "shadowed" term, but I did not mean to offend you by asking about it. I will google it. Syntax is a weakness for me. I use SysManager *myMgr; in the code. to me it means SysManager is the top level class. *myMgr is a pointer to an instance of that class. I do not know how to make the SysManager class merge with qt_object or with any Qt class, if that is even possible. The sFnd lib has other classes (IPort, INode, among others). I have code from online tutorials that show me how to write a class with private and public functions. When I try such code snippets, I get errors that say I am wrongly redefining class and/or class members. I really learn best with example code. I have yet to see any code from google search where an external shared library class is actually used in a Qt app in the way that I am trying to use this. Maybe my approach is all wrong. I did find another person who was struggling with this, but there was no resolution posted to his nearly identical questions. He asks:
                how is it possible that it works on VS and not in QT, arent the libraries independent from the Development environment? I will keep looking...

                jsulmJ 1 Reply Last reply
                0
                • C CharlesHuff

                  Thank you. I realize my questions are dumb.
                  I did a tutorial on classes. I can write basic c++ code outside of Qt that runs ok. No other person/tutorial has used the "shadowed" term, but I did not mean to offend you by asking about it. I will google it. Syntax is a weakness for me. I use SysManager *myMgr; in the code. to me it means SysManager is the top level class. *myMgr is a pointer to an instance of that class. I do not know how to make the SysManager class merge with qt_object or with any Qt class, if that is even possible. The sFnd lib has other classes (IPort, INode, among others). I have code from online tutorials that show me how to write a class with private and public functions. When I try such code snippets, I get errors that say I am wrongly redefining class and/or class members. I really learn best with example code. I have yet to see any code from google search where an external shared library class is actually used in a Qt app in the way that I am trying to use this. Maybe my approach is all wrong. I did find another person who was struggling with this, but there was no resolution posted to his nearly identical questions. He asks:
                  how is it possible that it works on VS and not in QT, arent the libraries independent from the Development environment? I will keep looking...

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @CharlesHuff You put

                  SysManager *myMgr;
                  

                  in constructor of your class. And you probably have added it also in your class declaration as member of your class, like:

                  class MainWindow
                  {
                  private:
                      SysManager *myMgr;
                  

                  Do you see what the problem is?
                  Your using another myMgr in your constructor which has no relation to the one declared in your class! This is what @SGaist means with "shadowing": myMgr in constructor hides ("shadows") myMgr declared as class member.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • C Offline
                    C Offline
                    CharlesHuff
                    wrote on last edited by
                    #9

                    I see class only used once in my whole Qt 6 project. Inside main window.h :
                    class MainWindow : public QMainWindow statement is just before Q_OBJECT macro? followed by the
                    MainWindow constructor and destructor...

                    I didn't know to put SysManager *myMgr; there? and in fact I did not put it there....

                    Confused is my middle name.

                    jsulmJ 1 Reply Last reply
                    0
                    • C CharlesHuff

                      I see class only used once in my whole Qt 6 project. Inside main window.h :
                      class MainWindow : public QMainWindow statement is just before Q_OBJECT macro? followed by the
                      MainWindow constructor and destructor...

                      I didn't know to put SysManager *myMgr; there? and in fact I did not put it there....

                      Confused is my middle name.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @CharlesHuff I'm also confused by what you write to be honest.
                      Can you please post your MainWindow class declaration (from the header file)?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        So we have the following situation:

                        • Variable scopes understanding issues (class members VS function local variables)
                        • Use of a singleton class

                        @CharlesHuff:

                        You did not offend me, you are new to C++ and this is a language that requires some work to be comfortable with hence my suggestion to get the basics correctly before diving further. There's a lot of code using Qt with external libraries. Qt itself uses external libraries. The whole KDE projects uses external libraries. The one you are currently using does not differ.
                        .
                        That said:

                        • you can't declare a variable in one function and use it in a different one (function local variables)
                        • I you need something reusable in a class, make it a class member
                        • You have a singleton object so it makes little sense to store a member variable for it
                        • When using a singleton, you grab it when needed in the functions that requires it,.
                          Why ? because the object may have been destroyed and recreated somewhere else in your code and thus your variable will point to some random memory.
                        • Don't try to use static variables for that, it's the wrong use case.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • C Offline
                          C Offline
                          CharlesHuff
                          wrote on last edited by
                          #12

                          @jsulm said in Qt help:

                          MainWindow

                          I can post it. It would be the exact same code that you get with a plain widget project. Your question lead me to move SysManager *myMgr; declaration there. like below:
                          '''
                          #ifndef MAINWINDOW_H
                          #define MAINWINDOW_H

                          include <QMainWindow"

                          #include <../../inc/inc-pub/pubSysCls.h>
                          QT_BEGIN_NAMESPACE
                          namespace ui { class MainWindow; }
                          {
                          Q_OBJECT
                          public: MainWindow(Widget *parent = nullptr);
                          ~MainWindow();

                          sFnd::SysManager *myMgr;
                          //above line is where you hinted me to put initialization of myMgr object so that one instance can be used
                          //in each on_button_clicked call...
                          //this seems to be working now. YAY! Thank you very much!

                          private slots:
                          void on_pushButtonInit_clicked();
                          private:
                          Ui::MainWindow *ui;
                          };
                          #endif // MAINWINDOW.H
                          '''

                          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