Embedded linux Qt object question



  • Hello,

    We are developing a device which is using Linux as OS with QT as framework.

    The development is done in QT Creator, we create a mainWindow and some form(widgets), but we have a class where we gonna make an object who needs to availble in all widgets.

    We worked something out and we have like to ask if this is the correct way:

    class ALways : public Object
    {
     Q_OBJECT
    public:
     explicit Always(QObject *parent = 0);
     void text(QString text);
    }
    Always::Always(QObject *parent) : QObject(parent)
    {
    }
    
    void Always::text(QString text)
    {
     qDebug() << text;
    }
    

    I have then I from called local and do the next:

    #include "always.h"
    #include "mainwindow.h"
    
    class Local : public QWidget
    {
     QObject
    
    public:
     explicit Local(QWidget *parent = 0);
     ~Local();
    
     MainWindow *mainWindow;
     Always *always;
    
    private slots:
     void on_pushButton_clicked();
    
    private:
     Ui::Local *ui;
    
    protected:
     void closeEvent(QCloseEvent *event);
    }
    
    Local::Local(QWidget *parent) : QWidget(parent), ui(new Ui::Local)
    {
     ui->setupUi(this);
    }
    Local::~Local()
    {
     delete ui;
    }
    void Local::on_pushButton_clicked()
    {
     always->text("I gonna close");
     this->setAttribute(Qt:WA_DeleteOnClose);
     this->close();
    }
    
    void Local::closeEvent(QCloseEvent *event)
    {
     this->mainWindow->show();'
     event->accept();
    }
    

    In the Main window I do the following:

    #include "always.h"
    
    class MainWindow : public QMainWindow
    {
     Q_OBJECT
    
    public:
     explicit MainWindow(QWidget *parent = 0);
     ~MainWindow;
    
    private slots:
     void on_pushButton_clicked();
    
    private:
     Ui:MainWindow *ui;
    }
    
    #include "local.h"
    
    Local *local;
    Always *always;
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui:MainWindow)
    {
     ui->setup(this);
    
     always = new Always(this);
    }
    MainWindow::~MainWindow()
    {
     delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
     local = new Local();
    
     local->mainWindow = this;
     local->always = always;
     local->show();
     local->setFocues();
     this->hide();
    }
    

    Is this are correct to share the object through the widgets?

    Please any feedback is welcome.

    Thanks in advanced


  • Lifetime Qt Champion

    Hi,

    Not really no, if the object must be really absolutely unique you should consider the singleton pattern.

    Why do you need Local to be static ?



  • Hello,

    Thanks I will have look into that...

    I did found this solution on the internet, because when I close a widget I would like to go back to the previous widget where I came from. Is there another way to do it?

    So when I do the following:
    MainWindow -> pushbutton to widget 1
    Widget 1 -> pushbutton to widget 2
    Widget 2 -> pushbutton to close widget 2 and go back to widget 1
    and when closing widget 1 I must go back to the MainWindow.

    I also saw solution that the made the class extern but I could not believe that this was a good way.

    Thank for the help



  • " MainWindow -> pushbutton to widget 1
    Widget 1 -> pushbutton to widget 2
    Widget 2 -> pushbutton to close widget 2 and go back to widget 1
    and when closing widget 1 I must go back to the MainWindow. "

    if this is what you want , then cant you just use signals and slots.
    you many not need to share objects.

    eg:
    void MainWindow::on_pushButton_clicked()
    {
    local = new Local();
    local->show();

    connect(local, SIGNAL(destroyed(QObject*)), this , SLOT(show()));
    this->hide();
    }



  • Hello,

    Thanks for the reply.

    I have tried this, but it doesn't seem to work, when Local is closed, the MainWindow doesn't come on and the application will close.

    So it looks like the destroyed signal is not to be excuted from the local, what can be wrong?

    thanks in advanced


  • Lifetime Qt Champion

    Wouldn't a QStackedWidget/Layout be more suited for that ?



  • Hello,

    I have looked in that, but I'm not sure if this will work in our design, but I can be wrong off course. In the code example I show the use of pushbuttons on the widget. The actual design will not have any LCD screen, and only have 8 keypad.

    Whit this keypad it is possible to select items from a main menu Widget(these are stored in listwidget). For example 'Explore USB drive' is a item from the main menu, when you select this, another widget will be opened and set focus to so the keypad will be active in this widget.

    Then you can explore the USB drive and copy files or delete files etc etc. When you are done, you must go back to the first widget in this case the main menu Widget, where you then can choose from the main menu.

    Every menu item will open a new widget, and will set focus to so the keypad is active on this widget, when you are done you must go back to the main widget.

    In example code what I did get, they make a widget extern:

    File: usbmedia.h
    #include < QWidget>
    #include <QListWidgetItem>
    
    namespace Ui{
    class UsbMedia;
    }
    class UsbMedia : public QWidget
    {
     QObject
    
    public:
     explicit UsbMedia(QWidget *parent = 0);
     ~UsbMedia();
    
     void m_show();
    
    private:
     Ui::UsbMedia *ui;
    ..
    ..
    };
    
    extern UsbMedia *usb_media;
    

    For my idea this extern here is not the correct idea, it works but, for my feeling this is not the way.

    Thanks in advance..



  • Hello,
    After my last post I did some coding and would like some comment if this the correct way.
    I have made 3 widget forms. Named: formOne, formTwo, formThree. On every .ui form I added a listwidget. Which is disabled, because I use a key event to select the items. In the example code, somethings are not there for space savings..

    ------------------------------------------------------------------
    File: formone.h
    #include <QWidget>
    #include <QKeyEvent>
    #include "formtwo.h"
    
    class FromOne : public QWidget
    {
      Q_OBJECT
    
      public explicit FormOne(QWidget *parent = 0);
      ~FormOne();
    
    private slots:
      void myShow();
    
    private:
     Ui::FormOne *ui;
    
     bool event(QEvent *event);
     void loadItems();
     void focusItem(QListWidgetItem *item);
    };
    
    File: formeone.cpp
    #include "formone.h"
    ..
    FormOne::FormOne(Widget *parent) : 
             QWidget(parent), ui(new Ui::FormOne)
    {
      ui->setupUI(this);
     
      this->loadItems;
    }
    
    //Private slot
    void FormOne::myShow()
    {
      this->loadItems();
      this->setFocus();
      this->show();
    }
    
    void FormOne::focusItem(QlistWidget *item)
    {
      if(item->text() == "FormTwo") {
        FormTwo * fromtwo = new FormTwo;
        formtwo->setAttribute(Qt::WA_DeleteOnClose);
        formtwo->setFocus();
        formtwo->show();
        connect(formtwo, SIGNAL(destroyed(Object *)), this, SLOT(myShow()));
      }
    }
    
    void FormOne::loadItems()
    {
      QListWidgetItem *item;
    
     if(ui->listWidget->count() > 0)
        ui->listWidget->clear();
    
      item = new QListWidgetItem
      item->setText("FormTwo");
      ui->listWidget->additem(item);
    
      item = new QListWidgetItem
      item->setText("Nothing");
      ui->listWidget->additem(item);
    
      ui->listWidget->setCurrentRow(0);
    }
    
    bool FormOne::event(QEvent *event)
    {
      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
      int index=0, size=0;
      
      if(event->type() != QEvent::KeyRelease)
        return QWidget::event(event);
    
      switch(keyEvent->key()) {
      case Qt::Key_N:
        index = ui->listWidget->currentRow();
        size = ui->listWidget->count();
        if(index < (size-1))
          index++;
        else
          index = 0;
       ui->listWidget->setCurrentRow(index);
       break;
      case Qt::Key_O:
        this->focusItem(ui->listWidget->currentItem());
        break;
      default:
        break;
    
      return QWidget::event(event);
    }
    ------------------------------------------------------------------
    File: formtwo.h
    #include <QWidget>
    #include <QKeyEvent>
    #include "formthree.h"
    
    class FormTwo: public QWidget
    {
      Q_OBJECT
    
      public explicit FormTwo(QWidget *parent = 0);
      ~FormTwo();
    
    private slots:
      void myShow();
    
    private:
     Ui::FromTwo *ui;
    
     bool event(QEvent *event);
     void loadItems();
     void focusItem(QListWidgetItem *item);
    };
    
    File: formtwo.cpp
    #include "formtwo.h"
    ..
    FormTwo::FormTwo(Widget *parent) : 
             QWidget(parent), ui(new Ui::FromTwo )
    {
      ui->setupUI(this);
     
      this->loadItems;
    }
    
    //Private slot
    void FormTwo::myShow()
    {
      this->loadItems();
      this->setFocus();
      this->show();
    }
    
    void FormTwo::focusItem(QlistWidget *item)
    {
      if(item->text() == "FormThree") {
        FormThree* formthree= new FormThree;
        formthree->setAttribute(Qt::WA_DeleteOnClose);
        formthree->setFocus();
        formthree->show();
        connect(formthree, SIGNAL(destroyed(Object *)), this, SLOT(myShow()));
        this->hide();  //hide form two
      }
    }
    
    void FormTwo::loadItems()
    {
      QListWidgetItem *item;
    
     if(ui->listWidget->count() > 0)
        ui->listWidget->clear();
    
      item = new QListWidgetItem
      item->setText("FormThree");
      ui->listWidget->additem(item);
    
      item = new QListWidgetItem
      item->setText("Nothing");
      ui->listWidget->additem(item);
    
      ui->listWidget->setCurrentRow(0);
    }
    
    bool FormTwo::event(QEvent *event)
    {
      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
      int index=0, size=0;
      
      if(event->type() != QEvent::KeyRelease)
        return QWidget::event(event);
    
      switch(keyEvent->key()) {
      case Qt::Key_N:
        index = ui->listWidget->currentRow();
        size = ui->listWidget->count();
        if(index < (size-1))
          index++;
        else
          index = 0;
       ui->listWidget->setCurrentRow(index);
       break;
      case Qt::Key_O:
        this->focusItem(ui->listWidget->currentItem());
        break;
      case Qt::key_E;
        this->close();
        break;
      default:
        break;
    
      return QWidget::event(event);
    }
    ----------------------------------------------------------
    File: formthree.h
    #include <QWidget>
    #include <QKeyEvent>
    
    
    class FormThree: public QWidget
    {
      Q_OBJECT
    
      public explicit FormThree(QWidget *parent = 0);
      ~FormThree();
    
    private:
     Ui::FormThree*ui;
    
     bool event(QEvent *event);
     void loadItems();
     void focusItem(QListWidgetItem *item);
    };
    
    File: formthree.cpp
    #include "formthree.h"
    ..
    FormThree::FormThree(Widget *parent) : 
             QWidget(parent), ui(new Ui::FormOne)
    {
      ui->setupUI(this);
     
      this->loadItems;
    }
    
    void FormThree::focusItem(QlistWidget *item)
    {
      if(item->text() == "DoSomething") {
        .. do something..
      }
    }
    
    void FormThree::loadItems()
    {
      QListWidgetItem *item;
    
     if(ui->listWidget->count() > 0)
        ui->listWidget->clear();
    
      item = new QListWidgetItem
      item->setText("DoSomething");
      ui->listWidget->additem(item);
    
      item = new QListWidgetItem
      item->setText("Nothing");
      ui->listWidget->additem(item);
    
      ui->listWidget->setCurrentRow(0);
    }
    
    bool FormThree::event(QEvent *event)
    {
      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
      int index=0, size=0;
      
      if(event->type() != QEvent::KeyRelease)
        return QWidget::event(event);
    
      switch(keyEvent->key()) {
      case Qt::Key_N:
        index = ui->listWidget->currentRow();
        size = ui->listWidget->count();
        if(index < (size-1))
          index++;
        else
          index = 0;
       ui->listWidget->setCurrentRow(index);
       break;
      case Qt::Key_O:
        this->focusItem(ui->listWidget->currentItem());
        break;
      case Qt::key_E;
        this->close();
        break;
      default:
        break;
    
      return QWidget::event(event);
    }
    

    Is this the way how I canopen and closes forms?

    Thanks in advance..


Log in to reply
 

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