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. -
@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. -
@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(); }
-
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
-
@o6a6r9v1p tabdialog is a pointer, what do you want to achieve if you write tabdialog(this)?
-
@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.
-
@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
-
@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).
-
@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 -
@o6a6r9v1p Posting images usually does not work here. You can upload it to a sharing service and post the link here.