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.
Related
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++.
Before taking up the main subject, please mind that i`m a beginner of Qt.
I made a AddIm.cpp, and I want to set an image on QLabel in MainWindow.
here is my source in AddIm.cpp
void AddIm::on_pushButton_clicked()
{
MainWindow mainwindow;
mainwindow.setImage();
}
and here is MainWindow.cpp
void MainWindow::setImage()
{
QPixmap pix("./test.jpg");
ui->label->setPixmap(pix);
}
and MainWindow.h
class MainWindow : public QMainWindow
{
public:
void setImage();
~ some source ~
private:
Ui::MainWindow *ui;
};
it doesn't work at all. so I added a button in MainWindow for testing.
and when it clicked, setImage works. but when I execute setImage in AddIm.
it doesn't work. please let me know why
Your problem has nothing to do with your knowledge with Qt but rather your knowledge of c++.
In AddIm::on_pushButton_clicked(), you create a new MainWindow object on the stack, create the image, and then exit the function.
When a function exits, all local stack objects are destroyed. This means that your image is indeed being loaded but the window is being destroyed before you get a chance to see it. Even if it survived to live longer than the function allowed, you never show the window so it remains hidden.
UPDATE:
Change AddIm.cpp to be the following:
void AddIm::on_pushButton_clicked()
{
MainWindow *mainwindow = new MainWindow;
mainwindow->setAttribute(Qt::WA_DeleteOnClose, true);
mainwindow->setImage();
mainwindow->show();
}
Compare this with with your code to see where you might have gone wrong. I tried as much as possible to code it just like you did. I hardly use the designer, i like to code everything. It works as expected.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QLabel>
#include <QPixmap>
class MainWindow : public QWidget {
Q_OBJECT
public:
void setImage();
private:
QLabel *label;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::setImage() {
QPixmap pix(":/test.jpg");
label = new QLabel;
label->setPixmap(pix);
label->show();
}
addim.h
#ifndef ADDIM_H
#define ADDIM_H
#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>
#include "mainwindow.h"
class AddIm : public QMainWindow {
Q_OBJECT
public:
AddIm(QWidget *parent = 0);
~AddIm();
private slots:
void on_pushButton_clicked();
private:
QPushButton *button;
};
#endif // ADDIM_H
addim.cpp
#include "addim.h"
AddIm::AddIm(QWidget *parent) : QMainWindow(parent) {
button = new QPushButton("Show Image");
setCentralWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
}
void AddIm::on_pushButton_clicked() {
MainWindow mainwindow;
mainwindow.setImage();
}
AddIm::~AddIm() {
}
main.cpp
#include "addim.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
AddIm window;
window.show();
return a.exec();
}
You did not shown the window.
First, you have to create a C++ Class not a single .cpp file. Then add a pointer to the window in your AddIm.h file:
private:
MainWindow* mainwindow;
Then in your AddIm.cpp file:
mainwindow = new MainWindow(this);
mainwindow->setAttribute(Qt::WA_DeleteOnClose, true); // prevent memory leak when closing window
mainwindow->setImage();
mainwindow->show();
And remember to include MainWIndow in AddIm.h
#include "mainwindow.h"
I have a small problem with adding widget to QMainWindow.
When i do it like that:
wsk_mainStatki = new mainStatki(this);
wsk_mainStatki ->setGeometry(0,0,400,300);
this->layout()->addWidget(wsk_mainStatki);
it's ok but i get warning:
QMainWindowLayout::addItem: Please use the public QMainWindow API instead
this is my game class
#include "game.h"
game::game()
{
setGeometry(200, 200, 400, 300);
setWindowTitle("Statki");
wsk_mainStatki = new mainStatki(this);
wsk_mainStatki ->setGeometry(0,0,400,300);
this->layout()->addWidget(wsk_mainStatki);
}
game header
#ifndef WIDGET1_H
#define WIDGET1_H
#include "k_plansza.h"
#include "mainStatki.h"
#include "settings.h"
#include <QApplication>
#include <QMainWindow>
class game : public QMainWindow
{
public:
game();
~game() {};
private:
mainStatki *wsk_mainStatki;
settings *wsk_settings;
};
#endif // WIDGET1_H
mainstatki class
#include "mainstatki.h"
mainStatki::mainStatki(QWidget *parent){
setupUi(this);
connect(closeButton, SIGNAL(clicked()), parent, SLOT(close()));
}
mainstatki header
#ifndef MAINSTATKI_H
#define MAINSTATKI_H
#include <QWidget>
#include "ui_mainStatki.h"
class mainStatki : public QWidget, public Ui::mainStatki
{
Q_OBJECT
public:
mainStatki(QWidget *parent);
};
#endif // MAINSTATKI_H
How it should look?
I believe it means you are not expected to manually insert stuff into the layout of a QMainWindow, but instead use methods like addToolBar, setStatusBar or setCentralWidget. The layouting of your own widgets would happen in the centralWidget.
By the way, your mainStatki constructor is missing a call to the QWidget constructor. Unless you have a good reason not to do it, your constructor should rather look like this:
mainStatki::mainStatki(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
connect(closeButton, SIGNAL(clicked()), parent, SLOT(close()));
}
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);