×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Nokia Developer Expert
    Join Date
    Jun 2009
    Posts
    36

    Question QT Designer - UI widget access

    I confess to beign a Qt novice and rusty on C++.

    I've used QtDesigner4.6 on Ubuntu (works very well) to start developing a simple app for the N900. I designed a tabbed UI in a mainwindow widget and successfully built it with the default code (used new project -> Qt Designer form). This runs fine.

    Now I want to start developing the logic behind the UI. The Ui_Form.h contains all the required widgets along with the connect statements to join the slots/signals I created in the designer.

    The UI is instantiated in a file mainwindow.cpp.

    My question is how I can access the ui widgets from another file for instance to create additional signals/slots between the ui widgets and to set values and so on. In the mainwindow.cpp file accessing member functions can be done by dereferencing the pointer to the ui object.

    How do you add/connect additional signals/slots?
    How do I access the ui widgets in another cpp file? For instance I have a QButton which on click I want to call a function (in file DOF_Calculation.cpp) and then update a QLCDNumber widget in the UI.

    In mainwindow.cpp I have...

    MainWindow::MainWindow( QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    .....

    which is the original boiler plate code.

    One option is to add the slots/signals and functions I need into the ui-form.h class definition and modify MainWindow.cpp to use them. This means any UI changes in designer will overwrite my additions.

    I've tried using the template "void on_<widget name>_<signal name>(<signal parameters>);" in my DOF-calculation.h file but don't know what class to attach this too to capture the QButton click(). The DOF_functions class is defined as

    class DOF_functions : public QWidget, private Ui_MainWindow { QObject ....

    There must be some obvious way of a) accessing the instantiated ui components outside the scope of MainWindow.cpp and b) adding more action handing code for the ui widgets again in another class or file.

    Any ideas on how???

    BR David

  2. #2
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QT Designer - UI widget access


  3. #3
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QT Designer - UI widget access

    Quote Originally Posted by davidmct View Post
    How do I access the ui widgets in another cpp file? For instance I have a QButton which on click I want to call a function (in file DOF_Calculation.cpp) and then update a QLCDNumber widget in the UI.
    If you trying implement a MVC design pattern, then accessing View elements from Controller is a wrong design.

    Quote Originally Posted by davidmct View Post
    I've tried using the template "void on_<widget name>_<signal name>(<signal parameters>);" in my DOF-calculation.h file but don't know what class to attach this too to capture the QButton click().
    These names are utilized by QMetaObject::connectSlotsByName http://doc.trolltech.com/4.6/qmetaob...ectSlotsByName
    You cannot connect two independent objects with connectSlotsByName as one must be a parent of another.

    Quote Originally Posted by davidmct View Post
    There must be some obvious way of a) accessing the instantiated ui components outside the scope of MainWindow.cpp and b) adding more action handing code for the ui widgets again in another class or file.
    In general, widgets in main window (and ui object) are declared private, meaning you cannot access them from anything else, but from this class methods (or friend classes). And this is done for purpose to implement an abstraction concept.

  4. #4
    Nokia Developer Expert
    Join Date
    Jun 2009
    Posts
    36

    Re: QT Designer - UI widget access

    Hi,

    Thanks. I restructured my design to ensure the Ui and handlers were a child of the main window.

    There seems to be some interesting scoping issues which I can't work out. In the mainwindow constructor I can access the UI as ui.setupUI(this) or directly as I used multiple inheritance. Within the constructor I can access the UI widgets eg ui.widget.setvalue().

    However, I have private functions in the MainWindow class which when trying to access the UI fail to compile and complains of widget/ui being out of scope. I solved this by changing:

    void func (void){...} to void MainWindow::func(void) {....}

    even though func was a private function in MainWindow and should have been in scope.

    Any ideas.

    BR David

  5. #5
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QT Designer - UI widget access

    Quote Originally Posted by davidmct View Post
    Hi,
    Thanks. I restructured my design to ensure the Ui and handlers were a child of the main window.

    There seems to be some interesting scoping issues which I can't work out. In the mainwindow constructor I can access the UI as ui.setupUI(this) or directly as I used multiple inheritance. Within the constructor I can access the UI widgets eg ui.widget.setvalue().

    However, I have private functions in the MainWindow class which when trying to access the UI fail to compile and complains of widget/ui being out of scope. I solved this by changing:

    void func (void){...} to void MainWindow::func(void) {....}

    even though func was a private function in MainWindow and should have been in scope.

    Any ideas.

    BR David
    Hi David!

    It's very hard to follow you as you seems to be very new to C++. Can you attach some sample code I can look at?

  6. #6
    Nokia Developer Expert
    Join Date
    Jun 2009
    Posts
    36

    Question Re: QT Designer - UI widget access

    I am new to C++ but learning :-)

    Here's the various source files.
    - main.cpp : as original
    - mainwindow.h and cpp
    - ui_DOF_form.h : UI elements. truncated as post too long buy key parts left

    mainwindow.cpp has some comments in the code on problem areas
    1. ui reference not in scope of member function on_DOF...click() unless MainWindow:: included in definition
    2. scope issues with ui_DOF_Total_Result, this ... outside of MainWindow::MainWindow or destructor
    3. How to reference ui in ~MainWindow.
    4. MainWindow:n_CalculateDOFButton_click(void) doesn't seem tied to click event but maybe I need to call connect by name.

    Many thanks for your help.

    ------------ source --------------


    main.cpp
    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }


    mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QWidget>
    #include "ui_DOF_Form.h"

    class UI_MainWindow;
    class QLCDNumber;

    //namespace Ui {
    // class MainWindow;
    // //class DOF_functions;
    //}

    class MainWindow : public QMainWindow, private Ui::MainWindow {
    Q_OBJECT
    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    protected:
    void on_CalculateDOFButton_click();
    QLCDNumber *ui_DOF_Total_Result;

    private:
    Ui::MainWindow ui;


    };

    #endif // MAINWINDOW_H


    mainwindow.cpp

    #include <QtGui>
    #include "mainwindow.h"


    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    setupUi(this);

    // looks like ´this´ is only valid in the scope of this function
    ui_DOF_Total_Result = qFindChild<QLCDNumber*>(this,"DOF_Total_Result");

    }

    MainWindow::~MainWindow()
    {
    // how to delete Ui? &ui causes crash
    //delete this;
    }

    // when MainWindow:: not included then ui becomes out of scope...

    void MainWindow:n_CalculateDOFButton_click(void)
    {
    // try to get this called when button pressed. uic connection expected
    // lets set a value to play

    // The following 3 lines fail to compile...
    //DOF_Total_Result->value(10);
    //ui_DOF_Total_Result->display((int)10);
    //DOF_Total_Result.display((int) 10);
    ui.DOF_Total_Result->display((int) 10);

    return;

    ui_DOF_Form.h

    /********************************************************************************
    ** Form generated from reading UI file 'DOF_Form.ui'
    **
    ** Created: Tue Jan 5 09:31:05 2010
    ** by: Qt User Interface Compiler version 4.6.0
    **
    ** WARNING! All changes made in this file will be lost when recompiling UI file!
    ********************************************************************************/

    #ifndef UI_DOF_FORM_H
    #define UI_DOF_FORM_H

    ----- includes deleted as automatic and post too long ----

    QT_BEGIN_NAMESPACE

    class Ui_MainWindow
    {
    public:
    QWidget *centralWidget;
    QTabWidget *DOFCalcTabWidget;
    QWidget *DOCCalc;
    QLineEdit *FocalLengthLine;
    QLabel *label_4;
    QLabel *label_6;
    QDial *FocalLengthDial;
    QLCDNumber *DOF_Back_Result;
    QLabel *label;
    QLCDNumber *DOF_Front_Result;
    QLCDNumber *DOF_Hyperfocal_Result;
    QLabel *label_5;
    QLabel *label_3;
    QLabel *label_7;
    QLabel *label_2;
    QComboBox *AperatureValue;
    QLineEdit *DistanceInput;
    QLabel *label_11;
    QComboBox *DistanceUnits;
    QFrame *line_4;
    QLabel *label_12;
    QLabel *label_13;
    QLabel *label_14;
    QLabel *label_15;
    QLCDNumber *Near_DOF_Result;
    QLCDNumber *Far_DOF_Result;
    QLCDNumber *DOF_Total_Result;
    QFrame *line_5;
    QPushButton *CalculateDOFButton;
    QWidget *Settings;
    QComboBox *MeasurementUnitSelect;
    QLabel *label_8;
    QLabel *label_9;
    QLabel *label_10;
    QDoubleSpinBox *CoC_Input;
    QFrame *line;
    QFrame *line_2;
    QFrame *line_3;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *MainWindow)
    {
    if (MainWindow->objectName().isEmpty())
    MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    MainWindow->resize(590, 382);
    centralWidget = new QWidget(MainWindow);
    centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    DOFCalcTabWidget = new QTabWidget(centralWidget);
    DOFCalcTabWidget->setObjectName(QString::fromUtf8("DOFCalcTabWidget"));
    DOFCalcTabWidget->setGeometry(QRect(0, 0, 591, 321));
    DOCCalc = new QWidget();
    DOCCalc->setObjectName(QString::fromUtf8("DOCCalc"));
    FocalLengthLine = new QLineEdit(DOCCalc);
    FocalLengthLine->setObjectName(QString::fromUtf8("FocalLengthLine"));
    FocalLengthLine->setGeometry(QRect(130, 140, 91, 27));
    FocalLengthLine->setInputMethodHints(Qt::ImhFormattedNumbersOnly|Qt::ImhPreferNumbers);
    label = new QLabel(DOCCalc);
    label->setObjectName(QString::fromUtf8("label"));
    label->setGeometry(QRect(10, 10, 121, 21));
    DOF_Total_Result = new QLCDNumber(DOCCalc);
    DOF_Total_Result->setObjectName(QString::fromUtf8("DOF_Total_Result"));
    DOF_Total_Result->setGeometry(QRect(420, 120, 121, 23));
    CalculateDOFButton = new QPushButton(DOCCalc);
    CalculateDOFButton->setObjectName(QString::fromUtf8("CalculateDOFButton"));
    CalculateDOFButton->setGeometry(QRect(430, 10, 92, 27));
    DOFCalcTabWidget->addTab(DOCCalc, QString());
    Settings = new QWidget();
    Settings->setObjectName(QString::fromUtf8("Settings"));
    MeasurementUnitSelect = new QComboBox(Settings);
    MeasurementUnitSelect->setObjectName(QString::fromUtf8("MeasurementUnitSelect"));
    MeasurementUnitSelect->setGeometry(QRect(210, 80, 85, 27));
    CoC_Input = new QDoubleSpinBox(Settings);
    CoC_Input->setObjectName(QString::fromUtf8("CoC_Input"));
    CoC_Input->setGeometry(QRect(230, 140, 61, 27));
    CoC_Input->setDecimals(3);
    CoC_Input->setMaximum(10);
    CoC_Input->setSingleStep(0.01);
    CoC_Input->setValue(0.3);
    DOFCalcTabWidget->addTab(Settings, QString());
    MainWindow->setCentralWidget(centralWidget);
    menuBar = new QMenuBar(MainWindow);
    menuBar->setObjectName(QString::fromUtf8("menuBar"));
    menuBar->setGeometry(QRect(0, 0, 590, 24));
    MainWindow->setMenuBar(menuBar);
    mainToolBar = new QToolBar(MainWindow);
    mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
    MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
    statusBar = new QStatusBar(MainWindow);
    statusBar->setObjectName(QString::fromUtf8("statusBar"));
    MainWindow->setStatusBar(statusBar);

    retranslateUi(MainWindow);
    QObject::connect(FocalLengthDial, SIGNAL(sliderMoved(int)), FocalLengthLine, SLOT(update()));
    QObject::connect(CalculateDOFButton, SIGNAL(clicked()), DOFCalcTabWidget, SLOT(update()));
    QObject::connect(DistanceInput, SIGNAL(returnPressed()), CalculateDOFButton, SLOT(click()));
    QObject::connect(FocalLengthLine, SIGNAL(returnPressed()), CalculateDOFButton, SLOT(click()));
    QObject::connect(AperatureValue, SIGNAL(textChanged(QString)), CalculateDOFButton, SLOT(click()));

    DOFCalcTabWidget->setCurrentIndex(0);
    DistanceUnits->setCurrentIndex(2);


    QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
    ---------------- code deleted ----------------------
    } // retranslateUi

    };

    namespace Ui {
    class MainWindow: public Ui_MainWindow {};
    } // namespace Ui

    QT_END_NAMESPACE

    #endif // UI_DOF_FORM_H

  7. #7
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QT Designer - UI widget access

    I've fixed your code. designed.tar.gz

    Quote Originally Posted by davidmct View Post
    1. ui reference not in scope of member function on_DOF...click() unless MainWindow:: included in definition
    I've removed ui completely as it was not initialized any way. But read about Member functions:
    http://en.wikipedia.org/wiki/C%2B%2B...mber_functions

    Quote Originally Posted by davidmct View Post
    2. scope issues with ui_DOF_Total_Result, this ... outside of MainWindow::MainWindow or destructor
    I've removed ui_DOF_Total_Result as well. It's not needed as you can access DOF_Total_Result just by writing "DOF_Total_Result" in member function.

    Quote Originally Posted by davidmct View Post
    3. How to reference ui in ~MainWindow.
    It was just ui, but now it's removed.

    Quote Originally Posted by davidmct View Post
    4. MainWindow:n_CalculateDOFButton_click(void) doesn't seem tied to click event but maybe I need to call connect by name.
    Because name of the signal is "clicked", not "click" and you have to define the function as slot, not just as protected member.

  8. #8
    Nokia Developer Expert
    Join Date
    Jun 2009
    Posts
    36

    Re: QT Designer - UI widget access

    Brilliant.

    Works perfectly.

    I think my confusion on clicked() v click() came about because in the UI header file they are both associated with the button. A signal clicked() and a slot click().

    My understanding of the following ...
    QObject::connect(DistanceInput, SIGNAL(returnPressed()), CalculateDOFButton, SLOT(click()));

    is that on return being pressed a signal is emitted to the CalculateDOFButton click() slot.

    And in the following when the CalculateDOFButton is clicked a signal is emitted to the DOFCalcTabWidget->update() slot.

    QObject::connect(CalculateDOFButton, SIGNAL(clicked()), DOFCalcTabWidget, SLOT(update()));

    If that is the case then why isn't the SLOT CalculateDOFButton.click() rather than clicked()?

    I'll re-read the user guide on signals and slots again.

    Many thanks
    BR David

  9. #9
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QT Designer - UI widget access

    Quote Originally Posted by davidmct View Post
    Brilliant.

    Works perfectly.
    You are welcome.

    Quote Originally Posted by davidmct View Post
    My understanding of the following ...
    QObject::connect(DistanceInput, SIGNAL(returnPressed()), CalculateDOFButton, SLOT(click()));

    is that on return being pressed a signal is emitted to the CalculateDOFButton click() slot.
    That's correct.

    Quote Originally Posted by davidmct View Post
    And in the following when the CalculateDOFButton is clicked a signal is emitted to the DOFCalcTabWidget->update() slot.

    QObject::connect(CalculateDOFButton, SIGNAL(clicked()), DOFCalcTabWidget, SLOT(update()));
    Correct.

    Quote Originally Posted by davidmct View Post
    If that is the case then why isn't the SLOT CalculateDOFButton.click() rather than clicked()?
    Because

    Code:
    void on_<object name>_<signal name>(<signal parameters>);
    
    QMetaObject::connectSlotsByName(this);
    is equivalent to

    Code:
    QObject::connect(<object name>, SIGNAL(<signal name>(<signal parameters>)),
                     this, SLOT(on_<object name>_<signal name>(<signal parameters>)));

Similar Threads

  1. Carbide.c++ UI Designer deffect when using s60 2nd edition sdk
    By nyamgasoft in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 10
    Last Post: 2008-08-21, 12:15
  2. Modifying Carbide UI designer code for view deactivation
    By andrew_ramsay in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2008-05-25, 09:26
  3. Carbide UI designer and different screen layouts
    By AlexOfBorg in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 2
    Last Post: 2007-03-18, 08:11
  4. UI designer problem
    By sujith in forum Tools and SDK Feedback (Closed)
    Replies: 0
    Last Post: 2006-11-13, 08:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •