November 23, 2011

lowee lowee
Lab Rat
16 posts

Translate UI file to .cpp and .h files

Page  
1

Hi there !

I created a UI file with Qt Designer and I would like to translate the ui file in .cpp and .h file.

I know that a .h file is generated at the compilation but I really want to translate it. I tried to copy paste some code of the .h file but it doesn’t really work…

And I think it’s better to create my own interface with code than with QtDesigner (you really know what you are doing…) and with a bit of reflection, it might be more easiest to have access to some object in the interface… (like QAction, QLineEdit, QCheckBox, etc.)

I hope my explanations are not bad…

So, to resume I would like to know how can I cut my .ui file to .cpp and .h file !!

Somebody can help me ?

 Signature 

We are animals that our brain gives us the feeling that we aren’t

16 replies

November 23, 2011

john_god john_god
Hobby Entomologist
234 posts

Hi
Your question is to much generic for a specific answer. Every component such as buttons, edit boxes, layouts are perfectly doable in code, you just have to learn how to do it. By opening the ui file as text, you will see the generated code, you can copy it to a new class file and make the necessary changes, perhapes this is the best way.

November 23, 2011

qxoz qxoz
Area 51 Engineer
602 posts

you can write all initialization manually.
Did you read
C++ GUI.Programming.with.Qt.4 by Jasmin Blanchette, Mark Summerfield
or
The.Art.of.Building.Qt.Applications by Daniel Molkentin

There are all information which you need.

or you can get converted code from ui_xxx.h file from build folder.

November 24, 2011

lowee lowee
Lab Rat
16 posts

Thanks (again) qxoz for those books, I’ll read them to perform my user interface and to code manually !

For the moment, I’ll convert the generated code from the build folder !

 Signature 

We are animals that our brain gives us the feeling that we aren’t

November 24, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3145 posts

Why would you want to copy the generated code into your project? The ui-files are so much nicer to update and everything!

You can access all the objects that are created in the generated header file just fine (provided you give good names in the designer) and can do all the parts that need extra intelligence in your own code without copying all the boilerplate code.

November 24, 2011

lowee lowee
Lab Rat
16 posts

Because I can’t change values of some objects… my interface is like this picture (added with the post just for 48 hours …)

Click here to see picture:http://www.hiboox.fr/go/images/informatique/1-interface-selection-config,e8d3fa92545554baae7cf57a20a701fe.bmp.html

and if a channel is selected (or more) in relation of labels, QLineEdit shall display channels selected and label selected…

 Signature 

We are animals that our brain gives us the feeling that we aren’t

November 24, 2011

BilbonSacquet BilbonSacquet
Lab Rat
75 posts

ok …

For the first question it’s easy, use ‘uic.exe your_interface.ui’ and pip the result to a file, after you could copy like you want.

BUT

It’s not true to think that it’s the best way, you do like this an hardcoding of your interface and any improvement will need lot of effort. But you don’t inherit or delegate the generate .h to you class? You have access to all and don’t need to copy paste anything.

Now you are saying that you couldn’t access to all you widgets, it’s too not true, you could access and set any widget values! For the selection problem you have to connect to the toggled() signal of the checkbox to do a for (all labels – probably QLineEdit objects) (*it)->selectAll();

And a small remark, I see an add label, which means you plan to add line, perhaps a QTable will be necessary!

November 24, 2011

qxoz qxoz
Area 51 Engineer
602 posts

I do not quite understand you
You want to display different groups of controls for different modes?

November 24, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3145 posts

Do use ui-files, put plain qwidgets with some layout wherever you need dynamic contents and then just add the custom widgets into those layouts. That minimizes the hardcoding you need to do.

November 24, 2011

qxoz qxoz
Area 51 Engineer
602 posts
Tobias Hunger wrote:
Do use ui-files, put plain qwidgets with some layout wherever you need dynamic contents and then just add the custom widgets into those layouts. That minimizes the hardcoding you need to do.

Yes that is most right solution.
I would like to offer the same thing.

November 25, 2011

Volker Volker
Robot Herder
5428 posts
Tobias Hunger wrote:
Do use ui-files, put plain qwidgets with some layout wherever you need dynamic contents and then just add the custom widgets into those layouts. That minimizes the hardcoding you need to do.

That’s exactly what I do regularly, it works like a charm.

Regarding your UI: You could consider using an item view and a custom model for your table.

November 28, 2011

lowee lowee
Lab Rat
16 posts

Thanks for all of your answers and I’m sorry to give you a later answer … (This weekend I was in a business trip and I couldn’t see your answers)

So during this trip, I do the translation (it works very well ^^), I agree with you (Tobias) when you talk about reducing hardcoding when using .ui file :

That minimizes the hardcoding you need to do

But to access to all my widgets, I will have too much functions like :

  1. on_rxCh0_ComboBox_toggled(bool toggle)
  2. on_rxCh1_ComboBox_toggled(bool toggle)
  3. on_rxCh2_ComboBox_toggled(bool toggle)
  4. on_rxCh3_ComboBox_toggled(bool toggle)

etc. (if I’m using the .ui file)

But my wish is (for example) :

  1. if Add_Label is clicked // Pushbutton
  2. {
  3.      if ((rxCh1 and rxCh2 is toggled) and (Label #022 is selected)) then
  4.              rxChannelEdit1 = "Channel 1" //1st QLineEdit of Channel column
  5.              rxChannelEdit2 = "Channel 2" //2nd QLineEdit of Channel column
  6.              (rxLabelEdit1 and rxLabelEdit2) = "Label #022" //1st and 2nd QLineEdit of Label column
  7. }

this is one of the differents possibilities I want to realize…

These explanations are more clear ?

 Signature 

We are animals that our brain gives us the feeling that we aren’t

November 28, 2011

lowee lowee
Lab Rat
16 posts

Just to give you my big problem (for the moment), I want to modify a QLineEdit (as explain forward) so that’s the function I wrote :

in mainwindow.h :

  1. private slots:
  2. bool on_rxCh0_CheckBox_toggled(bool checked);
  3. bool on_rxCh1_CheckBox_toggled(bool checked);
  4. bool on_rxCh2_CheckBox_toggled(bool checked);
  5. bool on_rxCh3_CheckBox_toggled(bool checked);
  6.  
  7. public:
  8. void set_rxChannel_Edit1(QLineEdit *rx_Channel_edit);
  9. void set_rxChannel_Edit2(QLineEdit *rx_Channel_edit);
  10. void set_rxChannel_Edit3(QLineEdit *rx_Channel_edit);
  11. void set_rxChannel_Edit4(QLineEdit *rx_Channel_edit);
  12.  
  13. private:
  14. QLineEdit *rx_Channel_1;
  15. QLineEdit *rx_Channel_2;
  16. QLineEdit *rx_Channel_3;
  17. QLineEdit *rx_Channel_4;

in mainwindow.cpp :

  1. void MainWindow::set_rxChannel_Edit1(QLineEdit *rx_Channel_edit)
  2. {
  3.      Ui_MainWindow::rx_Channel_edit_1 = rx_Channel_edit;
  4. }
  5.  
  6. void MainWindow::set_rxChannel_Edit2(QLineEdit *rx_Channel_edit)
  7. {
  8.      Ui_MainWindow::rx_Channel_edit_2 = rx_Channel_edit;
  9. }
  10.  
  11. void MainWindow::set_rxChannel_Edit3(QLineEdit *rx_Channel_edit)
  12. {
  13.      Ui_MainWindow::rx_Channel_edit_3 = rx_Channel_edit;
  14. }
  15.  
  16. void MainWindow::set_rxChannel_Edit4(QLineEdit *rx_Channel_edit)
  17. {
  18.      Ui_MainWindow::rx_Channel_edit_4 = rx_Channel_edit;
  19. }
  20.  
  21. [...]
  22.  
  23. void MainWindow::on_rxAddLabelButton_clicked()
  24. {
  25.     rx_Channel_1 = new QLineEdit;
  26.     rx_Channel_2 = new QLineEdit;
  27.     rx_Channel_3 = new QLineEdit;
  28.     rx_Channel_4 = new QLineEdit;
  29.  
  30.     if(ch0Checked){
  31.         if(ch1Checked){
  32.             if(ch2Checked){
  33.                 if(ch3Checked){
  34.                     set_rxChannel_Edit1(rx_Channel_1->setText(tr("#0")));
  35.                     set_rxChannel_Edit2(rx_Channel_2->setText(tr("#1")));
  36.                     set_rxChannel_Edit3(rx_Channel_3->setText(tr("#2")));
  37.                     set_rxChannel_Edit4(rx_Channel_4->setText(tr("#3")));
  38.                 }
  39.                 else{
  40.                     set_rxChannel_Edit1(rx_Channel_1->setText(tr("#0")));
  41.                     set_rxChannel_Edit2(rx_Channel_2->setText(tr("#1")));
  42.                     set_rxChannel_Edit3(rx_Channel_3->setText(tr("#2")));
  43.                 }
  44.             }
  45.             else{
  46.                 set_rxChannel_Edit1(rx_Channel_1->setText(tr("#0")));
  47.                 set_rxChannel_Edit2(rx_Channel_2->setText(tr("#1")));
  48.             }
  49.         }
  50.         else{
  51.             set_rxChannel_Edit1(rx_Channel_1->setText(tr("#0")));
  52.         }
  53.     }
  54. }
  55.  
  56. bool MainWindow::on_rxCh0_CheckBox_toggled(bool checked)
  57. {
  58.     return ch0Checked;
  59. }
  60.  
  61. bool MainWindow::on_rxCh1_CheckBox_toggled(bool checked)
  62. {
  63.     return ch1Checked;
  64. }
  65.  
  66. bool MainWindow::on_rxCh2_CheckBox_toggled(bool checked)
  67. {
  68.     return ch2Checked;
  69. }
  70.  
  71. bool MainWindow::on_rxCh3_CheckBox_toggled(bool checked)
  72. {
  73.     return ch3Checked;
  74. }

And the error generate is :

  1. .\API_A429-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\ui_mainwindow.h:-1: In member function 'void MainWindow::set_rxChannel_Edit1(QLineEdit*)':
  2. .\API_A429-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\ui_mainwindow.h:87: erreur : object missing in reference to 'Ui_MainWindow::rx_Channel_edit_1'
  3. .\API_A429-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\API_A429\mainwindow.cpp:57: erreur : from this location

this same error for the 4 functions set_rxChannel_Edit x so that’s why I think I can’t get access to my widgets… (cause I’m using .ui file …)

I tried to found some answers about those errors but I don’t…

PS: I think the way I coded those actions is … a bit barbarian …

 Signature 

We are animals that our brain gives us the feeling that we aren’t

November 28, 2011

fguimaraes fguimaraes
Lab Rat
21 posts

Hello lowee,

You could solve your problem connecting your checkboxes into a unique SLOT by using QSignalMapper [doc.qt.nokia.com].

In the follow example I’ve created 4 checkboxes and 4 labels in Qt Designer. The name of objects is a prefix (QCheckbox = “cb” and QLabel = “lb”) with a index number, working as suffix.

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QCheckBox>
  6. #include <QLabel>
  7. #include <QHash>
  8. #include <QSignalMapper>
  9.  
  10.  
  11. namespace Ui {
  12.     class MainWindow;
  13. }
  14.  
  15. class MainWindow : public QMainWindow
  16. {
  17.     Q_OBJECT
  18.  
  19. public:
  20.     explicit MainWindow(QWidget *parent = 0);
  21.     ~MainWindow();
  22.  
  23.  
  24. private slots:
  25.     void onStateChange (const QString& checkboxName);
  26.  
  27. private:
  28.     QHash<QString,QString> labelList;
  29.     Ui::MainWindow *ui;
  30.     QSignalMapper* signalMapper;
  31. };
  32.  
  33. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4.  
  5. #define CHECKBOX_PREFIX "cb"
  6. #define LABEL_PREFIX "lb"
  7. #define NUMBER_OF_CHECKBOXES 4
  8.  
  9. MainWindow::MainWindow(QWidget *parent) :
  10.     QMainWindow(parent),
  11.     ui(new Ui::MainWindow),
  12.     signalMapper (new QSignalMapper(this))
  13. {
  14.     ui->setupUi(this);
  15.     char indexName[33];
  16.     QString checkboxName;
  17.     QString labelName;
  18.     QCheckBox* checkbox;
  19.     QLabel* label;
  20.  
  21.  
  22.     for (int i = 0; i < NUMBER_OF_CHECKBOXES; ++i) {
  23.         itoa(i,indexName,10);
  24.         checkboxName.append(CHECKBOX_PREFIX).append(indexName);
  25.         labelName.append(LABEL_PREFIX).append(indexName);
  26.  
  27.         if ((checkbox = ui->centralWidget->findChild<QCheckBox* >(checkboxName)) && (label = ui->centralWidget->findChild<QLabel* >(labelName))){
  28.             //for label update
  29.             labelList[checkbox->objectName()] = labelName;
  30.  
  31.             //connect for further mapping
  32.             connect(checkbox, SIGNAL(stateChanged(int)), signalMapper, SLOT(map()));
  33.             signalMapper->setMapping(checkbox, checkboxName);
  34.             }
  35.  
  36.         labelName.clear();
  37.         checkboxName.clear();
  38.     }
  39.  
  40.     connect(signalMapper, SIGNAL(mapped(const QString& )),
  41.                  this, SLOT(onStateChange(const QString& )));
  42. }
  43.  
  44. MainWindow::~MainWindow()
  45. {
  46.     delete ui;
  47. }
  48.  
  49.  
  50. void MainWindow::onStateChange (const QString& checkboxName)
  51. {
  52.     QLabel* label;
  53.  
  54.     if (label = ui->centralWidget->findChild<QLabel* >(labelList[checkboxName])){
  55.         QCheckBox* checkbox =  ui->centralWidget->findChild<QCheckBox* >(checkboxName);
  56.  
  57.         switch (checkbox->checkState()){
  58.         case  Qt::Checked :
  59.             label->setText(tr("checked"));
  60.             break;
  61.  
  62.         case  Qt::Unchecked :
  63.             label->setText(tr("unchecked"));
  64.             break;
  65.  
  66.         case  Qt::PartiallyChecked :
  67.             label->setText(tr("PartiallyChecked"));
  68.             break;
  69.  
  70.         default :
  71.         break;
  72.  
  73.         }
  74.     }
  75. }

Any doubt, feel free to ask :)

 Signature 

Birth, death, rebirth, though, and constantly progress, that is the law.

November 29, 2011

lowee lowee
Lab Rat
16 posts

I think I understood (globally) … I just don’t understand those lines code but mostly the “if” condition

  1. if ((checkbox = ui->centralWidget->findChild<QCheckBox* >(checkboxName)) && (label = ui->centralWidget->findChild<QLabel* >(labelName))){
  2.             //for label update
  3.             labelList[checkbox->objectName()] = labelName;
  4.  
  5.             //connect for further mapping
  6.             connect(checkbox, SIGNAL(stateChanged(int)), signalMapper, SLOT(map()));
  7.             signalMapper->setMapping(checkbox, checkboxName);
  8.             }
  9.  
  10.         labelName.clear(); //here is just to reset the append() function ?
  11.         checkboxName.clear(); // idem ?

Your function

  1. onStateChange(const QString& checkboxName)
is just if checkbox is set to triState ? And can you explain your condition
  1. if (label = ui->centralWidget->findChild<QLabel* >(labelList[checkboxName])){
and your instruction (following the condition)
  1. QCheckBox* checkbox =  ui->centralWidget->findChild<QCheckBox* >(checkboxName);

Thanks, anyway, for your help !

 Signature 

We are animals that our brain gives us the feeling that we aren’t

November 29, 2011

fguimaraes fguimaraes
Lab Rat
21 posts

This onState SLOT, in this example, receives the SIGNAL emitted from checkbox, but could be connected to another SIGNAL if want to. The secret is that, through this first connect, I receive the SIGNAL from the object and, in the other one, I receive the object name.

  1.  
  2. onStateChange(const QString& checkboxName)

With the name of object, first, I verify if the related QLabel still exists.

  1. if (label = ui->centralWidget->findChild<QLabel* >(labelList[checkboxName]))

If the object exists, I create a QCheckBox object, with the findChild method to get the current state.

  1. QCheckBox* checkbox =  ui->centralWidget->findChild<QCheckBox* >(checkboxName);

Finally, with these two objects I’m able to make the change in the related object.

 Signature 

Birth, death, rebirth, though, and constantly progress, that is the law.

Page  
1

  ‹‹ [UI] Connection between QAction and SLOT      debug runs but release crashes on launch ››

You must log in to post a reply. Not a member yet? Register here!