Qt incorrectly lays widgets on top of each other - c++

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();
}

Related

C++ Qt inherit from custom widget

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!");
}

How to dynamically add widget inside a QStackedWidget

In order to replicate the problem I have I prepared a small verifiable example.
I have 2 QStackedWidgets inside a QGroupBox with a couple more components as shown below:
I created another widget called QBoxForm which carries a QComboBox only.
This last widget should appear on the QStackedWidget on the left as soon as the QCheckbox is ticked.
The QStackedWidget receive something because it becomes bigger but it does not show the QComboBox. How to make sure that a component is fully visible inside a QStackedWidget?
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
mCombo = new CBoxForm;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_checkBox_toggled(bool checked)
{
if(ui->checkBox->isChecked())
{
if(checked)
{
ui->stackedWidget->insertWidget(0, mCombo);
ui->stackedWidget->show();
}
}
if(!ui->checkBox->isChecked())
{
ui->stackedWidget->removeWidget(mCombo);
}
}
mainwindow.h
#include <QMainWindow>
#include "cboxform.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_checkBox_toggled(bool checked);
private:
Ui::MainWindow *ui;
CBoxForm *mCombo;
};
Lastly the additional combobox handled by another widget:
cboxform.h
#include <QWidget>
namespace Ui {
class CBoxForm;
}
class CBoxForm : public QWidget
{
Q_OBJECT
public:
explicit CBoxForm(QWidget *parent = nullptr);
~CBoxForm();
private:
Ui::CBoxForm *ui;
};
cboxform.cpp
CBoxForm::CBoxForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::CBoxForm)
{
ui->setupUi(this);
}
CBoxForm::~CBoxForm()
{
delete ui;
}
What I have done so far:
1) I followed the official documentation and applied the method insertWidget() as advised. In fact here is what I have done exactly. This part works and can be confirmed by the fact that the QStackedWidget become larger as I check the box.
Also consequently I remove the widget in a similar way applying removeWidget() method available in the official documentation.
Everything seems to follow the correct functioning, but the only missing part is that I don't understand why the QComboBox does not appear on the QStackedWidget as I followed precisely the official documentation.
Any idea on what I might be missing or forgot to include in the above code?
Thanks for pointing in the right direction for solving this problem.
You should add ui->stackedWidget->setCurrentIndex(0) after ui->stackedWidget->insertWidget(0, mCombo) to make it visible.

Adding .ui file to an existing class in QT

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);
}

How can I connect two windows in Qt?

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);
}
};

Creating a class that implements tabs and can be used as a "central widget" in QMainWindow

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.