Implementing Something like QDockWidget::togleViewAct for regular widgets - c++

just trying to get this working, I don't understand why it doesn't work. compiled with Qt Creator on Windows 7 x64, but I don't think that should matter too much.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QAction;
class QMenu;
class QWidget;
class mywidget;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private:
void createActions();
void createMenus();
mywidget *thewidget;
QMenu *viewMenu;
QAction *toggleViewAct;
};
#endif
mywidget.h:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class mywidget : public QWidget
{
public:
mywidget(QWidget *parent){ m_parent = parent ; }
private slots:
void toggleView(){ isHidden() ? setVisible(true) : setVisible(false) ; }
private:
QWidget *m_parent;
};
#endif
mainwindow.cpp:
#include <QtWidgets>
#include "mainwindow.h"
#include "mywidget.h"
MainWindow::MainWindow()
{
thewidget = new mywidget(new QWidget());
thewidget->setWindowTitle("test");
thewidget->setVisible(true);
createActions();
createMenus();
}
void MainWindow::createActions()
{
toggleViewAct = new QAction(tr("Toggle other widget"), this);
toggleViewAct->setStatusTip(tr("toggle tabbed widgets visibility"));
connect(toggleViewAct, SIGNAL(triggered()), thewidget, SLOT(toggleView()));
}
void MainWindow::createMenus()
{
viewMenu = menuBar()->addMenu(tr("&Menu"));
viewMenu->addAction(toggleViewAct);
}
main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("QtProject");
app.setApplicationName("Application Example");
MainWindow mainWin;
mainWin.show();
return app.exec();
}

Related

QT6 - signal/slot between 2 classes

I have created a new class for qpushbutton, I am trying to establish signal slot communication between this class and my mainwindow class
connect(&MyPushButton, &pushbutton::pb_isChecked, this, &MainWindow::MainWindowPBClicked, Qt::DirectConnection);
I used this code but as output only
qDebug("pushButtonClicked");
I can get this output. After that, the slot I called with "emit" does not work.
void pushbutton::pushButtonClicked()
{
if (isChecked())
{
qDebug("pushButtonClicked");
**emit pb_isChecked();**
}
}
all code in my project;
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow ui;
ui.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include "pushbutton.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void MainWindowPBClicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
pushbutton MyPushButton;
connect(&MyPushButton, &pushbutton::pb_isChecked, this, &MainWindow::MainWindowPBClicked, Qt::DirectConnection);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::MainWindowPBClicked()
{
qDebug("MainWindowPBClicked");
}
pushbutton.h
#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H
#include <QWidget>
#include <QPushButton>
class pushbutton : public QPushButton
{
Q_OBJECT
public:
explicit pushbutton(QWidget *parent = nullptr);
public slots:
void pushButtonClicked();
signals:
void pb_isChecked();
};
#endif // PUSHBUTTON_H
pushbutton.cpp
#include "pushbutton.h"
#include "mainwindow.h"
pushbutton::pushbutton(QWidget *parent)
: QPushButton{parent}
{
setAcceptDrops(true);
connect(this, &QPushButton::clicked, [this]() {
pushButtonClicked();
});
}
void pushbutton::pushButtonClicked()
{
if (isChecked())
{
qDebug("pushButtonClicked");
emit pb_isChecked();
}
}
CASE 1: Not creating button in UI file:
In your class MainWindow, variable MyPushButton is a local variable that gets destroyed after it end its scope in constructor. You need to create a dynamic variable of pushButton like this:
pushbutton *MyPushButton = new pushButton(this);
connect(MyPushButton, SIGNAL(pb_isChecked()), this, SLOT(MainWindowPBClicked()), Qt::DirectConnection);
CASE 2: Creating button in UI file:
In this case you cannot use your custom signal pb_isChecked(). You will need to use standard QPushButton signal such as clicked()
connect(ui->MyPushButton, SIGNAL(clicked()), this, SLOT(MainWindowPBClicked()), Qt::DirectConnection);

Qt make modification of MainWindow from another class

I would like to make some modifications of the main window from another file.
I created another ui file Form1Window (which open when a button is cliked in the MainWindow).
I want to call from the class Form1Window a function named test() of the MainWindow class
I succeed in calling function test() but I can't execute the whole content of the function (I can display a message but can't execute the part where I want to clear an edittext)
MainWindow.h
#include "form1window.h"
public slots:
void nettoyer();
private slots:
void openFrom1();
private:
Ui::MainWindow *ui;
From1Window *uiFrom1;
};
MainWindow.cpp
void MainWindow::openFrom1()
{
uiFrom1 = new From1Window(this);
uiFrom1->show();
}
void MainWindow::nettoyer(){
QMessageBox msgBox;
msgBox.setText("test");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
ui->auteur->clear();
//THIS LINE HAS NO EFFECT WHEN CALLED FROM THE OTHER CLASS
}
form1window.cpp
#include "mainwindow.h"
#include "ui_form1window.h"
void From1Window::on_supprimer_clicked()
{
MainWindow *a=new MainWindow ();
a->test();
close();
}
I've read about the role of the pointer of MainWindow class (C++ /Qt Proper way to access ui from another class in qt //Edited) and I've also tried connect()
Thank for your help
//THIS LINE HAS NO EFFECT WHEN CALLED FROM THE OTHER CLASS
this->ui->auteur->clear();
The line will never executed unless you dismiss QMessageBox. This is because you triggered QMessageBox with exec() function. This function has its own event queue and does not return until finishes. You may set QMessageBox as modal and display it with show() method. In that case QMessageBox will not block execution of the the flow.
This problem can also happen with QDialog(s) if you display them with exec().
I provide you a simple two window signal/slot example:
main.cpp
#include "mainwindow.h"
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form f;
f.show();
return a.exec();
}
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <QPushButton>
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
private:
QPushButton *pb;
};
#endif // FORM_H
form.cpp
#include "form.h"
#include "mainwindow.h"
#include <QDebug>
Form::Form(QWidget *parent) : QWidget(parent)
{
MainWindow *mw = new MainWindow();
pb = new QPushButton("clickME", this);
QObject::connect(pb, SIGNAL(clicked()), mw, SLOT(test()));
mw->show();
}
Form::~Form()
{
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void test();
private:
QLabel *l;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
l = new QLabel("test", this);
}
MainWindow::~MainWindow()
{
}
void MainWindow::test() {
qDebug() << "test called!" << endl;
l->setText("text changed");
}
This works for me.
output

How to connect a button from ui-file class to slots from other classes?

I have 3 classes.
class with a mainwindow which comes from the designer(ui-file)
class wich will manage database stuff like inserts
controller class. I want to extend the whole thing to networkcommunication later.
My problem:
I want to connect a simple QPushButton ui->addbutton from the window class with a slot addEntry from the databaseclass, but I get this error :
ERROR: no matching function for call to
'generalControler::connect(QPushButton*, const char*, dbmanager*&,
const char*)'
mydb, SLOT(addEntry()));
//no difference with &mydb or *mydb
MainWindow(0x13f57988, name = "MainWindow") QPushButton(0x13f5f3e0, name = "addButton")
MainWindow(0x13f57988, name = "MainWindow") 0x13f5f3e0//<--?????
//<=Here u see the adresses printed with Qdebug(). top: mainwindowclass. bot: generalcontrolerclass
//why there is missing information after returning the adress of a 'ui->addButton' to another class? Is this maybe the problem?
main.cpp
#include <QApplication>
#include "generalcontroler.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
generalControler controler;
return a.exec();
}
generalcontroler.h
#ifndef GENERALCONTROLER_H
#define GENERALCONTROLER_H
#include <QApplication>
#include "mainwindow.h"
#include "dbmanager.h"
class generalControler : public QObject
{
Q_OBJECT
public:
generalControler();
};
#endif // GENERALCONTROLER_H
generalcontroler.cpp
#include "generalcontroler.h"
#include <QDebug>
generalControler::generalControler(){
MainWindow* window = new MainWindow;
window->show();
dbmanager* mydb= new dbmanager("path_to_my_database.db", window);
mydb->addEntry();
qDebug()<<window->getThis()<<window->getcloseButton();
connect(window->getaddButton(), SIGNAL(clicked()),
mydb, SLOT(addEntry()));
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton* getaddButton();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
}
QPushButton* MainWindow::getaddButton()
{
return ui->addButton;
}
dbmanager.h
#ifndef DBMANAGER_H
#define DBMANAGER_H
#include <QSqlDatabase>
#include <QDebug>
#include "mainwindow.h"
class dbmanager: public QObject{
Q_OBJECT
public:
dbmanager(const QString& path);
public slots:
void addEntry();
private:
QSqlDatabase mydatabase;
};
#endif // DBMANAGER_H
dbmanager.cpp
#include "dbmanager.h"
dbmanager::dbmanager(const QString& path)
{
mydatabase = QSqlDatabase::addDatabase("QSQLITE");
mydatabase.setDatabaseName(path);
if (!mydatabase.open())
{
qDebug() << "Error: connection fail";
}
else
{
qDebug() << "Database: connection ok";
}
}
void dbmanager::addEntry()
{
qDebug()<<"addEntry success";
}
I was searching for 6 hours but I never saw such an example with 2 classes, a controler and an ui-file. Could anyone help me?
The connect looks good to me. Try if #include <QPushButton> in generalcontroler.cpp helps. If the compiler knows about QPushButton only by forward-declaration, it doesn't know that it's a QObject and thus the connect() signatures (with QObject* in it) don't match.

How to create object widget in other widgets in QT

In my application im having three widgets named as "Widget" , "one" and "two" . i tried to create the objects of the widget in main function as pass it as an argument to another widgets . it compiles successfully but application crashed before running , refer my code below and guide me,
//main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include "one.h"
#include "two.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w;
One *one;
Two *two;
w->GetObject(one,two);
one->GetObject(w,two);
two->GetObject(one,w);
w->show();
return a.exec();
}
//widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
void Widget::GetObject(One *onee, Two *twoo)
{
one=onee;
two=twoo;
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "ui_widget.h"
class One;
class Two;
class Widget : public QWidget, private Ui::Widget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
One *one;
Two *two;
void GetObject(One *onee, Two *twoo);
};
#endif // WIDGET_H
//one.cpp
#include "one.h"
One::One(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
void One::GetObject(Widget *widgett, Two *twoo)
{
widget = widgett;
two = twoo;
}
void One::on_pushButton_clicked()
{
widget->show();
}
//one.h
#ifndef ONE_H
#define ONE_H
#include "ui_one.h"
class Widget;
class Two;
class One : public QWidget, private Ui::One
{
Q_OBJECT
public:
explicit One(QWidget *parent = 0);
Widget *widget;
Two *two;
void GetObject(Widget *widgett, Two *twoo);
private slots:
void on_pushButton_clicked();
};
#endif // ONE_H

How to Widget inside another widget in QT?

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