I am designing a GUI without using only code and QLayout.
I have 2 Tabs, each with a QPushButton in them.
I already have the basic layout done and all that, and it's really basic, and will add functions to the QPushButtons later, just have them give a warning/message when clicked.
My problem is that I cannot seem to get the Widgets to resize with the window.
Here is my code:
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTabWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QPushButton *btn1;
QPushButton *btn2;
QTabWidget *widg;
};
#endif // WIDGET_H
and here is it's .cpp file
//widget.cpp
#include "widget.h"
#include <QLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent), btn1(new QPushButton("Button 1")), btn2(new QPushButton("Button 2")),
widg(new QTabWidget(this))
{
QWidget *w1(new QWidget);
QWidget *w2(new QWidget);
widg->addTab(w1, "TAB 1");
widg->addTab(w2, "TAB 2");
resize(200,200);
// w1->resize(this->width(),this->height());
QGridLayout *L1(new QGridLayout);
QGridLayout *L2(new QGridLayout);
L1->addWidget(btn1,0,0);
L2->addWidget(btn2,0,0);
w1->setLayout(L1);
w2->setLayout(L2);
}
Widget::~Widget()
{
}
I have looked online, but everything I found so far pointed to people using the ui-creator.
I am using Qt 6.3.0 C++.
Related
I am learning Qt using Qt 5.13 on MacOS.
First I define MyWidget inherited from QWidget. MyWidget has a QPushButton, but this button will be created in a slot function called 'fresh', not in constructor.
I add MyWidget in MainWindow (inherited from QMainWindow), and defined another button_2 to emit signal to callMyWidget's 'fresh' function to create button.
If I did not hide MyWidget in MainWindow first, MyWidget's button will not show. If I hide MyWidget first, everything seems OK.
I hope to know the reason. Thanks
I tried to repaint or update MyWidget in 'fresh' function, but did not help.
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include<QPushButton>
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = nullptr);
~MyWidget();
public slots:
void fresh();
private:
QPushButton* myButton;
};
#endif // WIDGET_H
mywidget.cpp
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
}
MyWidget::~MyWidget()
{
}
void MyWidget::fresh()
{
myButton = new QPushButton(this);
myButton->setStyleSheet("QPushButton { background-color: green;}");
show();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"mywidget.h"
#include<QHBoxLayout>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
signals:
public slots:
private:
MyWidget* myWidget;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
QWidget* qwidget = new QWidget;
myWidget = new MyWidget(this);
QPushButton* button = new QPushButton("Show",this);
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget(myWidget);
mainLayout->addWidget(button);
qwidget->setLayout(mainLayout);
setCentralWidget(qwidget);
//myWidget->hide();
connect(button,&QPushButton::clicked,myWidget,&MyWidget::fresh);
}
main.cpp
#include "mywidget.h"
#include"mainwindow.h"
#include<QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
If I add myWidget->hide(); in mainwindow.cpp, it seems right.
If I remove it, the green button will not show, even if I repaint or update or show in fresh function.
'mywidget' is taking the whole space, if you want to know if it is taking all the space or not :
mywidget->setStyleSheet("* {border: 4px solid orange;}")
Since you are using a layout, you might want to determine the minimum size of a QPushButton. the QPushButton has a default horizontal size policy : "Minimum", by default. Maybe if you set the minimum width using this function : "setMinimumWidth(int width)", might fix your problem.
Also, don't forget to call this :
myButton->show();
Every object that inherits from QObject should be shown with this func ".show".
Here is all the flags for QSizePolicy will help you understand what is going on in layouts (layouts work a lot with QSizePlicy flags) : https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum .
Unless you don't want the layout, you have to specify the position and the size in this way :
mywidget->setGeometry(QPoint(x, y), QSize(width, height));
and the same thing for your buttons.
When I run my project I can't use train_button to add lines in text. Because of I got this error:
QObject::connect: No such slot QTextEdit::onClick()
I try to solve it, but searched only information about adding Q_OBJECT, but I got this. My project is standart Qt Widget Application.
.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onClick(){
text->append("first\nsecond");
}
private:
QPushButton *train_button;
QTextEdit *text;
Ui::MainWindow *ui;
//QString a = "sdfsdfsdfsdf";
};
# endif // MAINWINDOW_H
.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
this->setFixedSize(800,600);
text = new QTextEdit(this);
train_button = new QPushButton(this);
text->setGeometry(50,50,500,500);
text->setPlaceholderText("Here we go ...");
train_button->setText("example");
train_button->setGeometry(600,50,100,50);
train_button->setStyleSheet( "background-color: rgb(0, 255, 0);border-style: inset;border-width: 0px;border-radius: 5px;border-color: beige;font: bold 14px;min-width: 10em; padding: 2px;" );
connect(train_button,SIGNAL(clicked()),text,SLOT(onClick();));
}
MainWindow::~MainWindow()
{
delete train_button;
delete solver_button;
delete text;
delete ui;
}
I use QMake version 3.0 using Qt version 5.2.1.
The error is quite clear:
No such slot QTextEdit::onClick()
The documentation is clear as well. QTextEdit has no onClick slot anywhere.
It's not clear what you're trying to do. In any case, you aren't doing it correctly: you cannot connect an inexistent slot to a signal.
By looking at your code, I see that you defined onClick as a member function of MainWindow.
Therefore probably this is what you want:
connect(train_button, &QPushButton::clicked, this, &MainWindow::onClick);
That is, probably you want to attach a slot of the class MainWindow to the button, not a slot of a QTextEdit.
I am new in Qt. And I am trying to open anther window from myMyMainWindow . I can't catch, what I am doing wrong with this situation. Don't want you guys to solve my problem, just say to please, what I am doing wrong.
So I have got a MainWindow.h( look at this comment, think you don't need to understand the whole proccess of it):
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
#include <QDialog>
#include <QMainWindow>
#include <QPushButton>
#include <QLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "timer.h"
class MyMainWindow: public QMainWindow
{
Q_OBJECT
private:
QPushButton *timer_Button;
QPushButton *StopWatch;
QPushButton *Close;
T_timer *myTimer;
public:
MyMainWindow(QWidget *parent);
public slots:
void Open_Timer_Window(); // Slot for opening a new window
};
#endif // MYMAINWINDOW_H
My MyMainWindow.cpp file:
#include "MyMainWindow.h"
MyMainWindow::MyMainWindow(QWidget *parent=0): QDialog(parent)
{
// just creating Buttons
timer_Button = new QPushButton ("Timer");
Close=new QPushButton("Close");
QHBoxLayout *Up=new QHBoxLayout;
Up->addWidget(timer_Button);
QHBoxLayout *Down=new QHBoxLayout;
Down->addWidget(Close);
QVBoxLayout *Main=new QVBoxLayout;
Main->addLayout(Up);
Main->addLayout(Down);
// the main part
connect(Close,SIGNAL(clicked()),this,SLOT(close()));
connect(timer_Button,SIGNAL(clicked()),this,SLOT(Open_Timer_Window()));// call `Slot of Open_Timer_Window()`
setLayout(Main);
setWindowTitle("Smart Watch");
}
void MyMainWindow::Open_Timer_Window()
{
myTimer = new T_timer(0);
myTimer->show();
}
So, I think I should show you the Second window, might be there is a mistake:
The header:
#include <QPushButton>
#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
class T_timer : public QDialog
{
Q_OBJECT
private:
QPushButton Start;
QPushButton Stop;
public:
T_timer(QWidget *parent=0);
};
And .cpp:
#include "timer.h"
T_timer::T_timer(QWidget *parent=0): QDialog(parent)
{
Start=new QPushButton ("Start");
Stop=new QPushButton ("Stop");
QHBoxLayout *Up=new QHBoxLayout;
Up->addWidget(Start);
Up->addWidget(Stop);
setLayout(Up);
}
Totally, I have got my MainWindow on the screen and after click a button timer , I haven't got an action.Help me please, if you can. Thanks.
It's a typo in your code. You need to read an output from your application to understand reasons.
connect(timer_Button,SIGNAL(click()clicked()),this,SLOT(Open_Timer_Window()));
I propose you to use Qt5 syntax.
I donĀ“t know why you're using QLayout in this case, I'd advise taking a look at this post: here
But to solve you problem try change you code to this:
connect(Close, SIGNAL(clicked(bool)), this, SLOT(close()));
connect(timer_Button, SIGNAL(clicked(bool)),this,SLOT(Open_Timer_Window()));
void MainWindow::Open_Timer_Window() {
Dialog dlg;
dlg.setModal(true);
dlg.show();
dlg.exec();
}
OBS: Change Dialog to your window.
I am trying to create layout like this
In the QT i created widgets and put in the main widget, the problem is not visible. none of the widget's are not shown. please help me to fix this issue
Complete Source Code
sandbox.ifuturemec.com/test.zip
Source code
mainWindow.cpp
#include "mainwindow.h"
#include <QtGui>
#include "headerbar.h"
#include <QGridLayout>
#include <QPushButton>
#include <QBoxLayout>
#include "statusbar.h"
#include "leftpanel.h"
#include "rightpanel.h"
#include "centerpanel.h"
mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
{
QGridLayout *layout=new QGridLayout(this);
headerBar *Header= new headerBar(this);
leftPanel *LeftPanel=new leftPanel(this);
centerPanel *CenterPanel=new centerPanel(this);
rightPanel *RightPanel=new rightPanel(this);
statusBar *Status=new statusBar(this);
QHBoxLayout *box=new QHBoxLayout();
box->addWidget(LeftPanel);
box->addWidget(CenterPanel);
box->addWidget(RightPanel);
layout->addWidget(Header,0,0);
layout->addLayout(box,1,0);
layout->addWidget(Status,2,0);
setLayout(layout);
}
mainWindow::~mainWindow() {}
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
class mainWindow : public QWidget
{
Q_OBJECT
public:
mainWindow(QWidget *parent = 0);
~mainWindow();
signals:
public slots:
};
#endif // MAINWINDOW_H
headerbar.cpp
#include "headerbar.h"
#include <QPushButton>
#include <QMessageBox>
headerBar::headerBar(QWidget *parent) : QWidget(parent)
{
this->setMaximumHeight(24);
this->setStyleSheet("background-color: rgb(85, 170, 255)");
}
headerBar::~headerBar(){}
headerbar.h
#ifndef HEADERBAR_H
#define HEADERBAR_H
#include <QWidget>
class headerBar : public QWidget
{
Q_OBJECT
public:
headerBar(QWidget *parent = 0);
~headerBar();
signals:
public slots:
};
#endif // HEADERBAR_H
Please help me to fix this problem.
Actually, your widgets do show ! But they are empty and have nothing to show !
Regarding the background color you are setting : the background-color property is not showing, because this property is not supported by QWidget.
Have a look at the documentation regarding this : List of stylable widgets.
More specifically :
QWidget : Supports only the background, background-clip and background-origin properties.
If you try to put, for instance, a label in your widgets, you'll see they do show :
centerPanel::centerPanel(QWidget *parent) :
QWidget(parent)
{
QHBoxLayout *box = new QHBoxLayout(this);
QLabel* pLabel = new QLabel("Center panel", this);
box->addWidget(pLabel);
this->setStyleSheet("background-color: rgb(85, 100, 100)");
}
If you just want the mainWindow to have a solid background color, you might try to just forget using the stylesheet and override the paintEvent method like this:
void mainWindow::paintEvent(QPaintEvent *event)
{
setPalette(QPalette(QColor(85, 170, 255)));
setAutoFillBackground(true);
}
hi how to add widget inside widget
i created main widget, and for the main widget headerbar come from another widget.
here the code below
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argl,char *argv[])
{
QApplication test(argl,argv);
mainWindow *window=new mainWindow();
window->setWindowState(Qt::WindowFullScreen);
window->show();
return test.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include <QtGui>
#include "headerbar.h"
#include <QGridLayout>
mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
{
QGridLayout *layout;
headerBar *Header=new headerBar(this);
layout->addWidget(Header,0,0);
this->setLayout(layout);
}
mainWindow::~mainWindow()
{
}
headerbar.cpp
#include "headerbar.h"
headerBar::headerBar(QWidget *parent) : QWidget(parent)
{
this->setMaximumHeight(24);
}
headerBar::~headerBar()
{
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
class mainWindow : public QWidget
{
Q_OBJECT
public:
mainWindow(QWidget *parent = 0);
~mainWindow();
signals:
public slots:
};
#endif // MAINWINDOW_H
headerbar.h
#ifndef HEADERBAR_H
#define HEADERBAR_H
#include <QWidget>
class headerBar : public QWidget
{
Q_OBJECT
public:
headerBar(QWidget *parent = 0);
~headerBar();
signals:
public slots:
};
#endif // HEADERBAR_H
while compile this code no errors. but when i am trying to run it's through error "exited with code -1073741819"
please help me to fix this issue
While you use layout, you have never created and assigned an instance to it:
QGridLayout *layout; // no initialization here
headerBar *Header = new headerBar(this);
layout->addWidget(Header,0,0); // layout is uninitialized and probably garbage
You should create it first before using it:
QGridLayout *layout = new QGridLayout(this);