could anyone tell me the steps on how to add a .ui file to an existing class in Visual?
Firstly I added a new item in Visual, then I chose "QT Dialog Form File" option and I then I created Dialog Form I desire in QT Designer.
My .h file:
(...)
#include "ui_Serial.h"
class Serial : public QWidget
{
Q_OBJECT
public:
Serial(QWidget *parent);
~Serial();
Ui::Serial *ui;
(...)
My problem is, that I can't use setup ui function:
Serial::Serial(QWidget *parent)
: QWidget(parent)
{
serial = new QSerialPort(this);
ui->setupUi(this);
}
I am getting this error:
cannot convert argument 1 from 'Serial *' to 'QDialog *
How I can get pass that?
Any ideas?
Greetings
Each Template has a default class because when the .ui is built, class commands are embedded.
If you use the template Widget your class should be QWidget.
If you use the template Dialog with Buttons Bottom, Dialog with Buttons Right, Dialog without Buttons your class should be QDialog.
If you use the template MainWindow your class should be QMainWindow.
So we conclude in your case that you should use a class that inherits from QDialog:
*.h
#include "ui_Serial.h"
class Serial : public QDialog
{
Q_OBJECT
public:
Serial(QWidget *parent=0);
~Serial();
Ui::Serial *ui;
}
*.cpp
Serial::Serial(QWidget *parent):QDialog(parent)
{
serial = new QSerialPort(this);
ui->setupUi(this);
}
Related
I'm working on a project with Qt and C++.
Now my Question is:
Is inheritance possible in UI classes?
For example: This is the Widget I want to inherit from
//windowA.h
namespace Ui {
class WindowA;
}
class WindowA : public QWidget
{
Q_OBJECT
public:
explicit WindowA(QWidget *parent = nullptr);
~AddWindow();
QPushButton *button;
};
//windowA.cpp
WindowA::WindowA(QWidget *parent) :
QWidget(parent)
{
button = new QPushButton();
button->setText("Save");
connect(button, SIGNAL (clicked()), this, SLOT (//slot));
QGridLayout *layout = new QGridLayout();
layout->addWidget(button, 0, 0);
this->setLayout(layout);
}
This is the widget that inherits from WindowA
//windowB.h
namespace Ui {
class WindowB;
}
class WindowB : public WindowA
{
Q_OBJECT
public:
explicit WindowB(QWidget *parent = nullptr);
~WindowB();
};
How would I implement a QPushButton, so that it's possible to set different text in both classes?
I can add a QPushButton but the text set in WindowA would also be set in WindowB. The problem is to set a different text for the button in WindowB than it is set for the button in WindowA
If I understand your question correctly, all you need to do is change what text you set on the button in your constructor:
WindowB::WindowB(QWidget *parent) :
WindowA(parent)
{
button->setText("Something else!");
}
I am trying make window with chat, and "main" window. If I click at username in chat window, it should show profile in main window. What's the best way to do something like this?
You should pass pointer to one window class from another and connect them by slots/signals:
class MainWindow
{
Q_OBJECT
...
public slots:
void onUsernameSelected(...);
};
class ChatWindow
{
Q_OBJECT
...
MainWindow *mainWindow;
...
ChatWindow(QObject *parent, MainWindow *mainWindow):
...
mainWindow(mainWindow)
{
connect(this, &ChatWindow::usernameSelected, mainWindow, &MainWindow::onUsernameSelected);
}
};
In Qt 4.8.5 32-bit and VS2010, I'm trying to create a Window as shown in this screenshot from QtDesigner:
When I run the application, the widgets get laid down on top of each other :
In the Console, I see this :
Does anybody have any idea why this is happening?
Here is my code :
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(){
ui = new Ui::AView();
ui->setupUi(this);
}
~MainWindow();
...
private:
Ui::AView* ui;
}
From the screenshot of QtDesigner, I see that your UI is for a QWidget, but you are applying it to a QMainWindow. This causes the problem, as the handling of main window is different to other widgets.
I don't know of a way to change the base class for a UI file. Maybe the best way is to create a new file, select the mainwindow template, and copy/paste the content from the old file.
Another option would be to have a QWidget, set it up with the UI, and add it as the main window's central widget:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(){
ui = new Ui::AView();
QWidget *wgt = new QWidget;
ui->setupUi(wgt);
this->setCentralWidget(wgt);
}
~MainWindow();
...
private:
Ui::AView* ui;
}
Try this:
class MainWindow : public QMainWindow, public Ui::AView
{
Q_OBJECT
public:
MainWindow(){
setupUi(this);
}
~MainWindow();
}
I have searched many forums to solve this problem so please don't just link a forum.
I have my main.ui that is loaded at the start. it's header
using namespace Ui;
class main: public QMainWindow
{
Q_OBJECT
public slots:
void AddPlayer();
public:
main(QWidget *parent = 0);
~main();
private:
Ui::mainClass ui;
Ui::FormPla uiPlayer;//the name of the form i want to open
};
Then i have the main.cpp
main::main(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.mainBtnPla, SIGNAL(clicked()), SLOT(AddPlayer()));
}
main::~main()
{
}
void FinalYearProject::AddPlayer()
{
//code to open player.ui
//formPla *obj;
//obj = new formPla();
//obj->show(); //i tried this but show wasnt a member of ui::player
}
player.ui doesnt have a .h or .cpp. I dont know if this affects this. I just want to be able open the form.
ui generated classes cannot be used without a QWidget class loading them.
You must create a separate QWidget base class (Player), loading Ui::FormPla and later create an instance of this class to show the Ui::FormPla on screen.
I want the central widget of my mainWindow class to be a QTabWidget. My plan is to create widgets that I want to put in the tabs as separate classes and add them to QTabWidget class and add the QtabWidget class itself as a central Widget to the mainWindow class.
To do this, how must I declare my tabWidget class ?
Should it be :
class centralTab : public QMainWindow
{
}
or
class centralTab : public QDialog
{
}
Also, in the ctor, What should be used as the parent ?
Just create QMainWindow subclass:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
//...
};
In constructor use addTab() method::
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QTabWidget *tabWidget = new QTabWidget(this);
//some settings
tabWidget->addTab(new QLabel("example1"), "General1");
tabWidget->addTab(new QLabel("example2"), "General2");
setCentralWidget(tabWidget);
}
Why QMainWindow? Because only QMainWindow has setCentralWidget() method;
You can add different widgets and set also icon to every tab, QLabel is just example.