Heap block at .. modified at .. past requested size of - c++

I try to create a window in another window with Qt and delete the first window. When the second window is opened the first window is deleted but when I try to delete the second window I get this error:
HEAP[Flash-TestBench-Client-Rev1-0.exe]:
Heap block at 18FBC298 modified at 18FBC2C4 past requested size of 24
Here is my simplified code:
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ConnectionWindow *w = new ConnectionWindow;
return a.exec();
}
connectionwindow.cpp
ConnectionWindow::ConnectionWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *w = new QWidget;
QVBoxLayout *l = new QVBoxLayout;
QPushButton *button = new QPushButton;
button->setText("Connect");
conenect(button, SINGNAL(pressed()), this, (openW()));
l->addWidget(button);
w->setLayout(l);
this->setCentralWidget(w);
this->setAttribute(Qt::WA_DeleteOnClose);
this->show;
}
void ConnectionWindow::openW(){
MainWindow *window = new MainWindow;
this->close;
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *w = new QWidget;
QVBoxLayout *l = new QVBoxLayout;
QPushButton *button = new QPushButton;
button->setText("Close");
conenect(button, SINGNAL(pressed()), this, (closeW));
l->addWidget(button);
w->setLayout(l);
this->setCentralWidget(w);
this->setAttribute(Qt::WA_DeleteOnClose);
this->show;
}
void MainWindow::closeW(){
this->close();
}

Related

display 2 QML files on a single QQuickwidget alternately when required without overlap

I have a QQuickwidget in a Qt C++ application where i have loaded a QML file (main.qml) and using QAction(actionstart) and C++ functions i have to load another QML file(main1.qml) slightly different to previous one on the same QQuickWidget object.
I am able to do this , but my 2nd QML file is overlapped from the middle section of QQuickwidget and further.
I have did this to stop overlapping of 2 QML files but not successful completely. count3 = 1 is defined in public section of Guiapplication.h file.
void GuiApplication::on_actionstart_triggered()
{
if (count3 == 1)
{
set_animation();
count3 = 2;
}
}
C++ Function for loading 1st QML file(main.qml)
void GuiApplication::rolling_animation()
{
QQuickView *quickWidget=new QQuickView();
QWidget *contain = QWidget::createWindowContainer(quickWidget,this);
contain->setMinimumSize(1008,349);
contain->setMaximumSize(1008,349);
contain->setFocusPolicy(Qt::TabFocus);
quickWidget->setSource(QUrl("qrc:/Resources/main.qml"));
ui->horizontalLayout_6->addWidget(contain);
}
C++ Function for loading 2nd QML file(main1.qml)
void GuiApplication::set_animation()
{
QQuickView *quickWidget=new QQuickView();
QWidget *Contain = QWidget::createWindowContainer(quickWidget,this);
Contain->setMinimumSize(1008,349);
Contain->setMaximumSize(1008,349);
Contain->setFocusPolicy(Qt::TabFocus);
quickWidget->setSource(QUrl("qrc:/Resources/main1.qml"));
ui->horizontalLayout_6->addWidget(Contain);
//ui->horizontalLayout_9->invalidate();
//ui->horizontalLayout_9->removeWidget(quickWidget_4);
}
output window image
Depending on whether you want to save or not the state of the QML that you want to hide there are the following alternatives:
Reject the same QQuickWidget (I recommend changing QQuickView to QQuickWidget) and just change the source.
#include <QtQuickWidgets>
class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent),
m_widget(new QQuickWidget)
{
m_widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
QPushButton *button1 = new QPushButton("show 1");
QPushButton *button2 = new QPushButton("show 2");
QHBoxLayout *lay = new QHBoxLayout(this);
QVBoxLayout *vlay = new QVBoxLayout;
vlay->addWidget(button1);
vlay->addWidget(button2);
lay->addLayout(vlay);
lay->addWidget(m_widget);
connect(button1, &QPushButton::clicked, this, &Widget::show1);
connect(button2, &QPushButton::clicked, this, &Widget::show2);
show1();
}
private slots:
void show1(){
m_widget->setSource(QUrl("qrc:/main1.qml"));
}
void show2(){
m_widget->setSource(QUrl("qrc:/main2.qml"));
}
private:
QQuickWidget *m_widget;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
Use 2 QuickWidgets and use a QStackedWidget to toggle the widgets, with this method only hidden so the state of the QML will persist, in the previous case when changing the source is lost.
#include <QtQuickWidgets>
class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent),
m_stacked_widget(new QStackedWidget)
{
for(const QString & url: {"qrc:/main1.qml", "qrc:/main2.qml"}){
QQuickWidget *widget = new QQuickWidget;
widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
widget->setSource(QUrl(url));
m_stacked_widget->addWidget(widget);
}
QPushButton *button1 = new QPushButton("show 1");
QPushButton *button2 = new QPushButton("show 2");
QHBoxLayout *lay = new QHBoxLayout(this);
QVBoxLayout *vlay = new QVBoxLayout;
vlay->addWidget(button1);
vlay->addWidget(button2);
lay->addLayout(vlay);
lay->addWidget(m_stacked_widget);
connect(button1, &QPushButton::clicked, this, &Widget::show1);
connect(button2, &QPushButton::clicked, this, &Widget::show2);
show1();
}
private slots:
void show1(){
m_stacked_widget->setCurrentIndex(0);
}
void show2(){
m_stacked_widget->setCurrentIndex(1);
}
private:
QStackedWidget *m_stacked_widget;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
The examples can be found here

Qt add menuBar, menus, and sub menus to QMainWindow

I have a hard time adding menu Bar, menus and sub menus to Qt QMainWindow programmatically.
The following code produces an error:
QWidget::setLayout: Attempting to set QLayout "" on QMainWindow "", which already has a layout
Notes :
*.The main window come out without any menu or Layout (Empty!)
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMenuBar *menu = new QMenuBar;
QMenu *file = new QMenu();
file->addMenu("&File");
menu->addMenu(file);
QSlider *s1 = new QSlider(Qt::Horizontal);
QSlider *s2 = new QSlider(Qt::Vertical);
QSpinBox *sb = new QSpinBox;
QHBoxLayout *L = new QHBoxLayout;
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
QMainWindow *w = new QMainWindow;
w->setLayout(L);
w->show();
return a.exec();
}
Each QMainWindow should have a central widget:
QMainWindow *w = new QMainWindow;
QWidget* centralWidget = new QWidget;
w->setCentralWidget( centralWidget );
centralWidget->setLayout(L);
w->show();
add layout to central widget:
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include<QMenuBar>
#include<QStatusBar>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *w = new QMainWindow;
QMenuBar *menu = new QMenuBar;
QMenu *file = new QMenu();
file->addMenu("&File");
menu->addMenu(file);
QWidget *centralwidget = new QWidget(w);
w->setCentralWidget(centralwidget);
QSlider *s1 = new QSlider(Qt::Horizontal, centralwidget);
QSlider *s2 = new QSlider(Qt::Vertical, centralwidget);
QSpinBox *sb = new QSpinBox;
QHBoxLayout *L = new QHBoxLayout(centralwidget);
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
w->show();
return a.exec();
}
This is the final version
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSlider *s1 = new QSlider(Qt::Horizontal);
QSlider *s2 = new QSlider(Qt::Vertical);
QSpinBox *sb = new QSpinBox;
QMainWindow *w = new QMainWindow;
QWidget *cw = new QWidget(w);
QMenuBar *menu = new QMenuBar(cw);
QHBoxLayout *L = new QHBoxLayout(cw);
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
QMenu *file = new QMenu("&File");
file->addMenu("Open");
file->addMenu("new");
QMenu *Build = new QMenu("&Build");
Build->addAction("Rebuild this file");
Build->addAction("Rebuild All");
menu->addMenu(file);
menu->addMenu(Build);
w->setCentralWidget(cw);
w->show();
QObject::connect (s1,SIGNAL(valueChanged(int) ), sb,SLOT(setValue(int) ) );
QObject::connect (s1,SIGNAL(valueChanged(int) ), s2,SLOT(setValue(int) ) );
QObject::connect (s2,SIGNAL(valueChanged(int) ), sb,SLOT(setValue(int) ) );
QObject::connect (s2,SIGNAL(valueChanged(int) ), s1,SLOT(setValue(int) ) );
QObject::connect (sb,SIGNAL(valueChanged(int) ), s1,SLOT(setValue(int) ) );
QObject::connect (sb,SIGNAL(valueChanged(int) ), s2,SLOT(setValue(int) ) );
return a.exec();
}
To try to clearly answer the question suggested by the title (which is not a question by itself), QMainWindow does already have an empty menu bar by default besides other things like a central widget.
To access this QMenuBar and populate it with things of your choice, just call menuBar() from your QMainWindow instance.
To add a submenu to a QMenuBar, use QMenuBar::addMenu.
For example :
QAction* newAct = new QAction("save");
auto fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
auto submenu = fileMenu->addMenu("Submenu");
submenu->addAction(new QAction("action1");
submenu->addAction(new QAction("action2");
For more information, look at this Qt example : https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html and also the QMenuBar reference https://doc.qt.io/qt-5/qmenubar.html#details
I realize this is an ancient post , but the answers are unnecessary complicated
basic Qt Widget application
QApplication a(argc, argv);
MainWindow w;
w.menuBar()->addAction("TEST");
w.show();
plus
addAction("TEST");
has 4 overloads to actually implement the menu option

Overlay widgets

I am trying to overlay a few buttons over my video player.
I have added a new class called overlay.cpp that subclassed a QWidget for the overlay purpose.
What I did in my code is to overlay button onto the video. In my centralWidget I have added a verticalLayout and morph it into a QWidget. The video was added into this verticalLayout. Upon program is running, the video is playing well. However, what's not working is the overlay of the button. The background doesn't seem to appear transparent even though it was set. I am not sure what is causing it to not appear transparent.
My code is as follows:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUI(this);
initializeVideo();
initializeButton();
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::initializeVideo(){
QVideoWidget *v_widget = new QVideoWidget;
QMediaPlayer *m_player = new QMediaPlayer;
m_player->setMedia(QUrl::fromLocalFile("C:/user/Desktop/video.wmv"));
m_player->setVideoOutput(v_widget);
ui->verticalLayout->addWidget(v_widget);
m_player->player();
v_widget->show();
}
void MainWindow::initializeButton(){
QFrame *b_frame = new QFrame;
QGridLayout *grid = new QGridLayout;
b_frame->setLayout(grid);
b_frame->setAttribute(Qt::WA_TranslucentBackground, true);
QPushButton *buttonStop = new QPushButton;
buttonStop->setText("STOP");
grid->addWidget(buttonStop, 0, 0, Qt::AlignTop);
overlay *overlay_1 = new overlay;
QGridLayout *gridLayout = new QGridLayout;
gridLayout->addWidget(b_frame);
overlay_1->setLayout(gridLayout);
overlay_1->setParent(ui->verticalWidget);
overlay_1->show();
b_frame->show();
}
overlay.cpp
#include "overlay.h"
overlay::overlay(QWidget *parent): QWidget(parent){
this->setAttribute(Qt::WA_TranslucentBackground, true);
}
Move declaration of QVideoWidget *v_widget and QMediaPlayer *m_player to mainwindow.h like this:
private:
Ui::MainWindow *ui;
QVideoWidget *v_widget;
QMediaPlayer *m_player;
In mainwindow.cpp:
void MainWindow::initializeVideo()
{
v_widget = new QVideoWidget(this);
m_player = new QMediaPlayer(this);
m_player->setMedia(QUrl::fromLocalFile("C:/user/Desktop/video.wmv"));
m_player->setVideoOutput(v_widget);
ui->verticalLayout->addWidget(v_widget);
m_player->play();
}
void MainWindow::initializeButton()
{
QGridLayout *grid = new QGridLayout(v_widget);
QPushButton *buttonStop = new QPushButton(this);
buttonStop->setText("STOP");
grid->addWidget(buttonStop, 0, 0, Qt::AlignTop);
}
This will add "STOP" button on top of QVideoWidget.
Specify widget's parent when crating it. new QVideoWidget(this) will create new QVideoWidget as child of current MainWindow widget. If you are creating child of already visible widget you do not need to call show() on it.

Custom Qt Widget

How do I create a simple widget that would contain a first, middle and last name field and add it to the main window class?
I ask because I cant figure out why is this simple widget attempt below is not working, what have I missed?
main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainWindow class
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
QMainWindow *mainView = new QMainWindow;
setCentralWidget(mainView);
CardUI *card = new CardUI;
QHBoxLayout *hCard = new QHBoxLayout;
hCard->addWidget(card);
mainView->setLayout(hCard);
mainView->show();
}
cardui class
CardUI::CardUI(QWidget *parent) : QWidget(parent){
QLineEdit *fnText = new QLineEdit;
QLineEdit *miText = new QLineEdit;
QLineEdit *lnText = new QLineEdit;
QHBoxLayout *name = new QHBoxLayout;
name->addWidget(fnText);
name->addWidget(miText);
name->addWidget(lnText);
setLayout(name);
}
QMainWindow *mainView = new QMainWindow;
//....
mainView->setLayout(hCard);
You should not change layout of QMainWindow. Use setCentralWidget or add toolbars/docks using given API instead.
In this particular case you shouldn't create mainView as QMainWindow (you cannot have two main windows in one application, right?). You can change mainView type to QWidget, but you can even don't create any proxy widgets, and just
MainWindow::MainWindow(QWidget *parent); : QMainWindow(parent){
card = new CardUI;
setCentralWidget(card);
}

QT separator widget?

Greetings all,
Is there any widget to separate two QWidgets and also give full focus to a one widget.
As shown in following figure ?
Thanks in advance,
umanga
How about QSplitter?
QWidget 1, for exmaple, QListView. QWidget 2 is a combination of QWidgets (the left part is simple QPushButton with show/hide caption, and the right part another widget)... All you have to do, is to hide your QWidget2 when user clicked on QPushButton...
If you need an example, I may post it.
Updated
main.cpp
#include "splitter.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
splitter w;
w.show();
return a.exec();
}
splitter.h
#ifndef SPLITTER_H
#define SPLITTER_H
#include <QtGui/QDialog>
class splitter : public QDialog
{
Q_OBJECT;
QWidget* widget1;
QWidget* widget2;
QPushButton* button;
public:
splitter(QWidget *parent = 0, Qt::WFlags flags = 0);
~splitter();
private slots:
void showHide(void);
};
#endif // SPLITTER_H
splitter.cpp
#include <QtGui>
#include "splitter.h"
splitter::splitter(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
QApplication::setStyle("plastique");
QListView* listView = new QListView;
QTableView* tableView = new QTableView;
button = new QPushButton("Hide >");
widget1 = new QWidget;
QHBoxLayout* w1Layout = new QHBoxLayout;
w1Layout->addWidget(listView);
w1Layout->addWidget(button);
widget1->setLayout(w1Layout);
widget2 = new QWidget;
QHBoxLayout* w2Layout = new QHBoxLayout;
w2Layout->addWidget(tableView);
widget2->setLayout(w2Layout);
QSplitter *mainSplitter = new QSplitter(this);
mainSplitter->addWidget(widget1);
mainSplitter->addWidget(widget2);
connect(button, SIGNAL(clicked()), this, SLOT(showHide()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(mainSplitter);
setLayout(mainLayout);
}
splitter::~splitter()
{}
void splitter::showHide(void)
{
if (widget2->isVisible())
{ // hide
widget2->setVisible(false);
button->setText("< Show");
}
else
{ // show
widget2->setVisible(true);
button->setText("Hide >");
}
}