How to customize a class, for use in our program



  • I have created a blank project in QT, trying to create GUI through program.
    This way I donot want to change ui_proj.h file.

    I need LineEdit box, a Label in a row. (put into a QHLayout)
    Below it I need a tabWidget, to display segregated info in different tabs.

    I tried the example given at
    http://doc.qt.io/qt-5/qtwidgets-dialogs-tabdialog-example.html
    it worked fine as a stand alone program.
    Now I want to adopt "TabDialog class" from that example in my project, so that I can put QHLayout and TabWidget into QVLayout .

    Name of my app's class is "demoapp" and namespace is "ui".
    where shall I change the program.


  • Moderators

    @o6a6r9v1p Usually you create your widgets in the constructor of the GUI class (I guess it is your main window).
    So, after ui->setupUi(this); you put the code to add needed widgets.



  • @jsulm
    Thanks for it. How to include classes from other examples/projects. How to call constructor of other class in our code. In the tabWidget example, 4 classes are used. Can we use them with out rewriting code.


  • Moderators

    @o6a6r9v1p Add the headers and cpp files containing those classes to your project (copy them into your project folder where your classes are) and use the classes. Don't forget to add those files to the pro file of your project, run qmake afterwards and rebuild. How to call a constructor of a class belongs to C++ basics:

    #include "SomeClass.h"
    
    MyClass::MyClass()
    {
        SomeClass *someClass = new SomeClass();
    }


  • @jsulm

    Did as told . This is code for main class.

    @

    DemoApp::DemoApp(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DemoApp)
    {
    TabDialog *tabdialog =new TabDialog();

    ui->setupUi(this);
    modifyUi(this);
    tabdialog(this);
    

    }

    @

    getting 'tabdialog' cannot be used as a function


  • Moderators

    @o6a6r9v1p tabdialog is a pointer, what do you want to achieve if you write tabdialog(this)?


  • Moderators

    @o6a6r9v1p TabDialog is a dialog, that means it is going to be its own window. If you want to put the stuff from TabDialog into your main window then do not add it to your project but copy its code and put it into your main window.



  • @jsulm
    TabDialog is a class used to create a tabbed window. That class is defined and available at above referred link it is top level class, which creates a tabbed window, by using three more classes for three tabs.



  • @o6a6r9v1p
    it uses tabWidget



  • @jsulm
    it increases size of main ctor.



  • @jsulm
    copying code into main window increases size of ctor. want to aviod that.


  • Moderators

    @o6a6r9v1p Well just put that code in a private method in your main window and call it in the constructor.
    The thing is: TabDialog is a dialog. Do you want to use it as such in your app (means: do you want to show a dialog window in your app)?



  • @jsulm
    TabDialog is a class constructor & is used to build top level Tabbed windows.
    This in turn uses 3 more classes, each class used to build one tab of the window.
    the code for TabDialog is
    @
    TabDialog::TabDialog(const QString &fileName, QWidget *parent)
    : QDialog(parent)
    {
    QFileInfo fileInfo(fileName);

    tabWidget = new QTabWidget;
    tabWidget->addTab(new DashBoardTab(fileInfo), tr("DashBoard"));
    tabWidget->addTab(new SettingsTab(fileInfo), tr("Settings"));
    tabWidget->addTab(new ValuesTab(fileInfo), tr("Values"));
    
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
                                     | QDialogButtonBox::Cancel);
    QLabel *fileNameLabel = new QLabel(tr("File Name:"));
    QLabel *label_Status = new QLabel(tr("Status"));
    label_Status->setObjectName(QString::fromUtf8("Status"));
    label_Status->setEnabled(true);
    label_Status->setGeometry(QRect(410, 10, 41, 21));
    label_Status->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    QLineEdit *deviceConnectedStatus = new QLineEdit();
    deviceConnectedStatus->setObjectName(QString::fromUtf8("deviceConnectedStatus"));
    deviceConnectedStatus->setEnabled(true);
    deviceConnectedStatus->setGeometry(QRect(20, 10, 391, 20));
    deviceConnectedStatus->setReadOnly(true);
    
    
    QHBoxLayout *statusLayout = new QHBoxLayout;
    statusLayout->addWidget(deviceConnectedStatus);
    statusLayout->addWidget(label_Status);
    
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    
    QVBoxLayout *mainLayout = new QVBoxLayout;
    
    mainLayout->addLayout(statusLayout);
    mainLayout->addWidget(tabWidget);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);
    
    setWindowTitle(tr("Device Monitor"));
    

    }

    @

    DashBoardTab(fileInfo) , SettingsTab(fileInfo), ValuesTab(fileInfo) are 3 classes for 3 tabs



  • @jsulm
    yes, i want to show dialog window.


  • Moderators

    @o6a6r9v1p In this case you should not create the TabDialog instance in the constructor of your main window. Instead do it when you want to show it (user clicks a button or menu or whatever).



  • @jsulm
    is it possible to start with 1st tab by default.
    Top row: device status
    below this Tabbed window is needed. it has to start with Tab0 default. As we click other tabs, we have to change.


  • Moderators

    @o6a6r9v1p
    Sorry, I don't understand your description: what is "Top row"? What is "device status"? First you ask whether it is possible to start with tab1 and then you say it has to start with Tab0, I'm confused.
    It is possible to activate a specific tab, read documentation: http://doc.qt.io/qt-5/qtabwidget.html#currentIndex-prop



  • @jsulm
    sorry, it is typing mistake. tab0 it is.
    How to add image here.
    I have created image in Paint, which i can post here


  • Moderators

    @o6a6r9v1p Posting images usually does not work here. You can upload it to a sharing service and post the link here.




  • Moderators

    @o6a6r9v1p Sure, it is possible.
    Check their code (constructor) and extend it like this:

    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *topRowLayout = new QHBoxLayout;
    QLineEdit *lineEdit = new QLineEdit(this);
    QLabel *label = new QLabel(this);
    topRowLayout.addWidget(lineEdit);
    topRowLayout.addWidget(label);
    mainLayout->addLayout(topRowLayout);
    mainLayout->addWidget(tabWidget);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);
    


  • @jsulm
    In side tabWidget, I need 3 tabs. In that example code, they have used separate classes for them. Now I will do as you suggested.

    In this, i am getting signal from abcd(child) class and it is to be connected to(parent class) updateGui() in this function.
    how to connect them.


  • Moderators

    @o6a6r9v1p You do not need to use separate classes for your tabs, but you can if you like. Please read documentation: http://doc.qt.io/qt-5/qtabwidget.html#details
    it is explained there:
    "Create a QTabWidget.
    Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
    Insert child widgets into the page widget, using layouts to position them as normal.
    Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut."

    How to connect signals/slots? What exactly is the problem? Do it in your parent class where you create the children, for example:

    tabWidget = new QTabWidget;
    DashBoardTab *dashBoardTab = new DashBoardTab(fileInfo);
    connect(dashBoardTab, SIGNAL(someSignal()), this, SLOT(someSlot()));
    tabWidget->addTab(dashBoardTab, tr("DashBoard"));
    tabWidget->addTab(new SettingsTab(fileInfo), tr("Settings"));
    tabWidget->addTab(new ValuesTab(fileInfo), tr("Values"));
    


  • @jsulm
    Thank you very much. Now I am able to get the tabs.


Log in to reply
 

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