I learn Qt. So, now I start make my mini-project and encountered in this issue – all my widgets shifting slightly right and down and my first two widgets also have a black space in the middle, why?
sorry, I don’t have Git, so I just Copy-Pasted the code.
headers:
#ifndef TOPAREACLASS_H
#define TOPAREACLASS_H
#include <QObject>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QComboBox>
#include <QLineEdit>
class topAreaClass : public QWidget
{
Q_OBJECT
public:
explicit topAreaClass(const QString& label,const QString& path, QWidget *parent = nullptr);
private:
QWidget* topArea;
QWidget* leftTopArea;
QHBoxLayout* topHBoxLayout;
QVBoxLayout* leftTopVBoxLayout;
QPixmap* m_pixLabelCurrency;
QLabel* m_currencyLabelPicture;
QLabel* m_nameOfCurrency;
QLineEdit* m_edit1;
};
#endif // TOPAREACLASS_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QComboBox>
#include <QLineEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
private:
QWidget* topArea;
QWidget* leftTopArea;
QHBoxLayout* topHBoxLayout;
QVBoxLayout* leftTopVBoxLayout;
QPixmap* m_pixLabelCurrency;
QLabel* m_currencyLabelPicture;
QLabel* m_nameOfCurrency;
QLineEdit* m_edit1;
};
#endif // WIDGET_H
cpp:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "topareaclass.h"
topAreaClass::topAreaClass(const QString& Label,const QString& path, QWidget *parent)
: QWidget{parent}
{
//topArea
topArea = new QWidget(this);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setFixedSize(500, 70);
setStyleSheet("background: grey;");
//leftTopArea
leftTopArea = new QWidget(topArea);
leftTopArea->setFixedSize(70, 70);
leftTopArea->setStyleSheet("background: grey;");
leftTopVBoxLayout = new QVBoxLayout(leftTopArea);
leftTopVBoxLayout->setContentsMargins(5,5,5,5);
//Flag
m_pixLabelCurrency = new QPixmap(path);
m_currencyLabelPicture = new QLabel();
m_currencyLabelPicture->setPixmap(m_pixLabelCurrency->scaled(60,30, Qt::KeepAspectRatio));
m_currencyLabelPicture->setAlignment(Qt::AlignCenter);
//Usd
m_nameOfCurrency = new QLabel(Label);
m_nameOfCurrency->setAlignment(Qt::AlignCenter);
leftTopVBoxLayout->addWidget(m_currencyLabelPicture);
leftTopVBoxLayout->addWidget(m_nameOfCurrency);
//edit1 (topRight)
m_edit1 = new QLineEdit("100000");
m_edit1->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
m_edit1->setStyleSheet("border: none;"
"font-size: 35px;"
"margin-right: 10px;");
m_edit1->setFixedHeight(70);
//QHBoxLayout
topHBoxLayout = new QHBoxLayout(this);
topHBoxLayout->setContentsMargins(0,0,0,0);
topHBoxLayout->addWidget(leftTopArea);
topHBoxLayout->addStretch();
topHBoxLayout->addWidget(m_edit1);
}
#include "widget.h"
#include "topareaclass.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(500,700);
setStyleSheet("background: black;");
setWindowTitle("Currency Convertor");
QVBoxLayout* mainLayout = new QVBoxLayout(this);
topAreaClass* top1 = new topAreaClass("USD", ":/new/prefix1/build/usa.png");
topAreaClass* top2 = new topAreaClass("UAH", ":/new/prefix1/build/uah.png");
mainLayout->addWidget(top1);
mainLayout->addWidget(top2);
mainLayout->addStretch();
}
Widget::~Widget() {}
how can I fix it? So there isnt any space around widgets?
New contributor
Oleksiy Rubakha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.