Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:38:53+00:00 2026-06-10T17:38:53+00:00

I have two windows: mainwindow.ui and dialog.ui . In the mainwindow.cpp I initialise some

  • 0

I have two windows: mainwindow.ui and dialog.ui. In the mainwindow.cpp I initialise some objects on the heap of another class for Ethernet-communication and use functions of these objects to read date from the bus and show the values in the mainwindow.ui. In dialog.ui I would like to set the values on the bus, but the problem is to access the communication-functions and objects in mainwindow.cpp.
I wanted to define dialog.cpp as friend class, but I make something wrong. Here is some code:

mainwindow.h

class MainWindow : public QMainWindow 
{protected: DateReg *myPosReg;...}

mainwindow.cpp

...   
myPosReg = new DateReg(DateValue->AddReg());
...
myPosReg->GetValue(a,b,c);
myPosReg->setValue(a,b,c);
    ...

Can I somehow access setValue() function in dialog.cpp? Should I allways use dialog.cpp for the dialog.ui or there is some possibility work with dialog.ui-date in mainwindow.cpp? To set the values in dialog.ui I use QDoubleSpinBox.
In the mainwindow.ui I call dialog.ui.
Excuse me for my English.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:38:54+00:00Added an answer on June 10, 2026 at 5:38 pm

    As far as I understand, you have two classes: MainWindow and Dialog. As MainWindow::myPosReg is protected, you can only access it in Dialog, if Dialog derives from MainWindow or if it is a friend.

    The following design should work:

    class MainWindow : public QMainWindow 
    {
        [...]
      protected: 
        DateReg *myPosReg; 
        friend class Dialog; 
    };
    
    class Dialog
    {
        [...]
        MainWindow* myMainWindow;
    
        void someFunction()
        {
            myMainWindow->myPosReg->setValue([...]);
        }
    };
    

    As an alternative, I would suggest a more Qt-ish way by using signals and slots. When the spin-boxes in Dialog change, emit a signal to which MainWindow listens.

    We start with the dialog:

    class Dialog : public QDialog
    {
        [...]
        Dialog()
        {
            [...init ui...]
            connect(ui->spinBoxA, SIGNAL(valueChanged(int)),
                    this, SLOT(onSpinBoxValueChanged()));
            connect(ui->spinBoxB, SIGNAL(valueChanged(int)),
                    this, SLOT(onSpinBoxValueChanged()));
            connect(ui->spinBoxC, SIGNAL(valueChanged(int)),
                    this, SLOT(onSpinBoxValueChanged()));
        }
    
      signals:
        void valuesChanged(int a, int b, int c);
    
      private slots:
        void onSpinBoxValueChanged()
        {
            emit valuesChanged(ui->spinBoxA->value(), ui->spinBoxB->value(),
                               ui->spinBoxC->value());
        }
    };
    

    So the dialog has an internal slot onSpinBoxValueChanged(), which is called when
    one of the spinboxes changes its value. The sole purpose of this slot is to emit
    another signal valuesChanged to which we’ll connect the main window (i.e. the
    dialog’s internal slot combines three values into one signal).

    The main window could be the following:

    class MainWindow : public QMainWindow
    {
        [...]
        DateReg *myPosReg;
      public slots:
        void setValues(int a, int b, int c)
        {
            myPosReg->setValues(a, b, c);
        }
    };
    

    And the code, which connects both

    int main()
    {
        [...]
        MainWindow* mainWindow = new MainWindow;
        Dialog* dialog = new Dialog;
        connect(dialog, SIGNAL(valuesChanged(int,int,int)),
                mainWindow, SLOT(setValues(int,int,int)));
    }
    

    When the user changes the value in a spinbox, the following chain-reaction happens:

    QSpinBox::valueChanged(int) -> Dialog::onSpinBoxValueChanged() 
      -> Dialog::valuesChanged() -> MainWindow::setValues() -> DateReg::setValues()
    

    The advantage is that you decouple MainWindow and Dialog: Dialog just signals that the values have changed but does not care what happens with them and MainWindow changes its myPosReg but does not care, where the values are coming from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Two windows MainWindow and Login. The button which shows login located on
I have created two windows in my project root folder, MainWindow.xaml and PopupWindow.xaml respectively.
Added detailed info I have two Windows.(mainWindow and childWindow.) On the top of the
I have two windows application, one is a windows service which create EventWaitHandle and
Let's say I have two windows services running. One service is 'Polling' - it
How can you combine two windows in Screen? I have two windows such that
I want to have two Emacs windows on the screen: one for Dired and
I have two installations of Windows 7. a 64bit version on my hard disk,
I have two versions of a Windows Forms Application and can't figure for the
Two Windows processes have memory mapped the same shared file. If the file consists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.