I have a database in a separate widget class datbase.ui that I insert as tab in mainwindow.ui. I want that when initializing the connection in the class datbase.ui statusbar in mainwindow.ui to change depending.
datbase.cpp
void DatBase::on_pushButton_clicked()
{
QSqlDatabase ddbb = QSqlDatabase::addDatabase("QSQLITE");
ddbb.setDatabaseName(".../db/ddberdbsqlite");
if (ddbb.open()){
// It is necessary that this status is displayed in mainwindow.ui
// How to get access to gui mainwindow?
ui->statusBar->showMessage("Successful connection!");
}
else{
ui->statusBar->showMessage("Not Successful connection!");
}
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "datbase.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Measurement DDEBR");
connect(ui->tabWidget, QTabWidget::currentChanged, this, MainWindow::showTabPageIndex);
DatBase *tabDatBase = new DatBase();
ui->tabWidget->insertTab(0, tabDatBase, "Data Base");
}
MainWindow::~MainWindow()
{
delete ui;
}
Related
I have three windows Mainwindow,firstwindow and secondwindow.I have a pushbutton in mainwindow,When i clicked the push button it opens the firstwindow.Up to this part everything is ok.
What i need is , I need to open secondwindow automaticaly after 2sec of the formation of firstwindow
What is happening now is when i clicking the push button in mainwindow, secondwindow will appear first,and the first window will form only after execution of second window.
I am using qt5 with qtcreator
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_clickMeButton_clicked()
{
FirstWindow * dlg = new FirstWindow;
dlg->show();
}
FirstWindow.cpp
#include "firstwindow.h"
#include "ui_firstwindow.h"
FirstWindow::FirstWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::FirstWindow)
{
ui->setupUi(this);
connect(this,SIGNAL(winMessage()),this,SLOT(openNewWindow()));
emit winMessage();
}
FirstWindow::~FirstWindow()
{
delete ui;
}
void FirstWindow::openNewWindow()
{
dlg = new SecondWindow;
dlg->show();
}
SecondWindow.cpp
**
#include "secondwindow.h"
#include "ui_secondwindow.h"
SecondWindow::SecondWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::SecondWindow)
{
ui->setupUi(this);
}
SecondWindow::~SecondWindow()
{
delete ui;
}
**
My understading is you want something like this:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QPushButton>
class FirstWindow : public QWidget
{
Q_OBJECT
public:
FirstWindow() : QWidget() { resize(100, 100); }
};
class SecondWindow : public QWidget
{
Q_OBJECT
public:
SecondWindow() : QWidget() { resize(100, 100); }
};
class MainWindow : public QPushButton
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include <QTimer>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QPushButton(parent)
{
setText("Click me");
connect(this, &MainWindow::clicked, this, [this] {
FirstWindow* first = new FirstWindow;
first->show();
QTimer::singleShot(2000, this, [] {
(new SecondWindow)->show();
});
});
}
If you prefer to separate the implementations, you can start the timer in the first window; the timer will give time to return to the event loop and actually show the first window.
#include <QTimer>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QPushButton(parent)
{
setText("Click me");
connect(this, &MainWindow::clicked, this, [] {
FirstWindow* first = new FirstWindow;
first->show();
});
}
FirstWindow::FirstWindow()
{
resize(100, 100);
QTimer::singleShot(2000, this, [] {
(new SecondWindow)->show();
});
}
or more precisely, you may wait for the actual show event:
#include <QTimer>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QPushButton(parent)
{
setText("Click me");
connect(this, &MainWindow::clicked, this, [] {
FirstWindow* first = new FirstWindow;
first->show();
});
}
void FirstWindow::showEvent(QShowEvent *)
{
QTimer::singleShot(2000, this, [] {
(new SecondWindow)->show();
});
}
I have a dial and a slider, in mainwindow.cpp I have:
ui->quickWidget_3->setSource(QUrl("qrc:///slider.qml"));
ui->quickWidget_4->setSource(QUrl("qrc:///dial.qml"));
I want that when I move slider dial moves..
I wrote:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtQml>
#include <QtQuick>
#include <QSlider>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setStyleSheet("background-color:white");
ui->quickWidget->engine()->rootContext()->setContextProperty("yourObject",ui->horizontalSlider);
ui->quickWidget_2->engine()->rootContext()->setContextProperty("yourObject1",ui->horizontalSlider_3);
QObject* item = ui->quickWidget_3->rootObject();
QObject::connect(item, SIGNAL(moved()), SLOT(sliderMoved()));
ui->quickWidget_2->setSource(QUrl("qrc:///sl.qml"));
ui->quickWidget->setSource(QUrl("qrc:///qml.qml"));
ui->quickWidget_3->setSource(QUrl("qrc:///slider.qml"));
ui->quickWidget_4->setSource(QUrl("qrc:///dial.qml"));
ui->horizontalSlider->setStyleSheet("QSlider::groove:vertical {background-color:red; position:absolute; left:4px; right: 4px}");
//connect circularGauge e text
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_horizontalSlider_2_sliderMoved(int position)
{
ui->dial->setValue(position);
}
void MainWindow::sliderMoved()
{
QObject* slider = ui->quickWidget_3->rootObject();
QObject* dial = ui->quickWidget_4->rootObject();
qreal value = QQmlProperty::read(slider, "value").toReal();
QQmlProperty::write(dial, "value", value);
}
but it doesn't work
It is well documented here: Interacting with QML Objects from C++
Please try to provide full relevant working code in your questions next time, like I'm going to do in this answer. Doing so, you will increase the chance of an answer.
test.pro
QT += core gui widgets quickwidgets quickcontrols2
[...]
dial.qml
import QtQuick.Controls 2.12
Dial {
id: control
from: 0
to: 100
stepSize: 1
value: 0
}
slider.qml
import QtQuick.Controls 2.12
Slider {
id: control
from: 0
to: 100
stepSize: 1
value: 0
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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 sliderMoved();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include <QQmlProperty>
#include <QQuickItem>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->quickWidget_1->setSource(QUrl("qrc:/slider.qml"));
ui->quickWidget_2->setSource(QUrl("qrc:/dial.qml"));
QObject* item = ui->quickWidget_1->rootObject();
QObject::connect(item, SIGNAL(moved()), SLOT(sliderMoved()));
}
void MainWindow::sliderMoved()
{
QObject* slider = ui->quickWidget_1->rootObject();
QObject* dial = ui->quickWidget_2->rootObject();
qreal value = QQmlProperty::read(slider, "value").toReal();
QQmlProperty::write(dial, "value", value);
}
MainWindow::~MainWindow()
{
delete ui;
}
First of all, I created about 20 push buttons using for loop. And named them using if else loop. Now, I want to connect each push buttons with the new dialog box. If I had used the design mode of QT, it would show me the name of the button when I press connect(ui->pushButton_0, SIGNAl(released()), SLOT(digit_pressed()) something like this. But, I don't know the name of the pushbutton I made as the for and if else loop made it. The connect(ui-> .......) also doesn't show any predictions. How can I link these push buttons and a new dialog box?
Here is my code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QVBoxLayout>
#include "amputation.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->)
QPixmap pix("/home/skanda/Desktop/D4564.png");
ui->label_2->setPixmap(pix);
setWindowTitle("First Aid");
QVBoxLayout *lay = new QVBoxLayout();
int i;
for(i=0;i<25;i++)
{
if(i==0){
QPushButton *button = new QPushButton("Amputation");
lay->addWidget(button);
}
else if(i==1){
QPushButton *button = new QPushButton("Asthama");
lay->addWidget(button);
}
else if(i==2){
QPushButton *button = new QPushButton("Bleeding");
lay->addWidget(button);
}
else if(i==3){
QPushButton *button = new QPushButton("Burns");
lay->addWidget(button);
}
else if(i==4){
QPushButton *button = new QPushButton("Chest Pain");
lay->addWidget(button);
}
else if(i==5){
QPushButton *button = new QPushButton("Diarrhoea");
lay->addWidget(button);
}
else if(i==6){
QPushButton *button = new QPushButton("Drowning");
lay->addWidget(button);
}
else if(i==7){
QPushButton *button = new QPushButton("Epilepsy");
lay->addWidget(button);
}
else if(i==8){
QPushButton *button = new QPushButton("Fainting");
lay->addWidget(button);
}
else if(i==9){
QPushButton *button = new QPushButton("Fever");
lay->addWidget(button);
}
else if(i==10){
QPushButton *button = new QPushButton("Food Poisoning");
lay->addWidget(button);
}
else if(i==11){
QPushButton *button = new QPushButton("Fracture");
lay->addWidget(button);
}
else if(i==12){
QPushButton *button = new QPushButton("Head Injury");
lay->addWidget(button);
}
else if(i==13){
QPushButton *button = new QPushButton("Muscle Strain");
lay->addWidget(button);
}
else if(i==14){
QPushButton *button = new QPushButton("No breathing");
lay->addWidget(button);
}
else if(i==15){
QPushButton *button = new QPushButton("Nose bleed");
lay->addWidget(button);
}
else if(i==16){
QPushButton *button = new QPushButton("Poisoning");
lay->addWidget(button);
}
else if(i==17){
QPushButton *button = new QPushButton("Snake Bites");
lay->addWidget(button);
}
else if(i==18){
QPushButton *button = new QPushButton("Stroke");
lay->addWidget(button);
}
else if(i==19) {
QPushButton *button = new QPushButton("Sun Burn");
lay->addWidget(button);
}
else if(i==20) {
QPushButton *button = new QPushButton("Testicle Pain");
lay->addWidget(button);
}
else if(i==21) {
QPushButton *button = new QPushButton("Ulcer");
lay->addWidget(button);
}
else if(i==22) {
QPushButton *button = new QPushButton("Child delievery");
lay->addWidget(button);
}
else if(i==23) {
QPushButton *button = new QPushButton("Heart Attack");
lay->addWidget(button);
}
else {
QPushButton *button = new QPushButton("Gastric");
lay->addWidget(button);
}
}
ui->scrollContents->setLayout(lay);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked(){
Amputation amp;
amp.setModal(true);
amp.show();
}
**In these codes, I have tried my luck by creating on_pushButton_clicked() function. But, it was just a try. **
As in programming everything is cooking, :), let's see what are the ingredients of connect():
connect(sender, &Sender::signal, receiver, &Receiver::slot);
so sender would be the buttons, the signal is the clicked, the receiver itself, that is, this, and the slot on_pushButton_clicked
I see unnecessary if-else, everything can be reduced to a for:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QVBoxLayout>
#include "amputation.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix("/home/skanda/Desktop/D4564.png");
ui->label_2->setPixmap(pix);
setWindowTitle("First Aid");
QVBoxLayout *lay = new QVBoxLayout();
QStringList names{"Amputation", "Asthama", "Bleeding", "Burns", "Chest Pain",
"Drowning", "Diarrhoea", "Epilepsy", "Fainting", "Fever",
"Food Poisoning", "Fracture", "Head Injury", "Muscle Strain",
"No breathing", "Nose bleed", "Poisoning", "Snake Bites",
"Stroke","Sun Burn", "Testicle Pain", "Ulcer", "Child delievery",
"Heart Attack", "Gastric"};
for(const QString & name: names)
{
QPushButton *button = new QPushButton(name);
lay->addWidget(button);
connect(button, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked)
}
ui->scrollContents->setLayout(lay);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked(){
Amputation amp;
amp.setModal(true);
amp.show();
}
Note:
avoid using the old style of connection, has many disadvantages, read the content of the following link for more information:
https://wiki.qt.io/New_Signal_Slot_Syntax
Make all these QPushButton members of a class such that they are created and destroyed properly rather than creating them in an loop.
Once you have each one as a separate member, then have a method InitializeConnections() and make all the connections under it using the Qt connect syntax.
If you believe these buttons are part of the MainWindow class, then your class can look something like this :
class MainWindow{
...
...
private :
// Will make connections of each button to it's respective slot.
void InitializeConnections();
private :
QPushButton *mAmputationButton;
QPushButton *mAsthmaButton;
//.. so on
};
And in MainWindow.cpp :
#include <QPushButton>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
mAmputationButton(new QPushButton("Amputation") ),
mAsthmaButton(new QPushButton("Asthama") )
{
InitializeConnections();
}
void MainWindow::InitializeConnections()
{
connect(mAmputationButton, &QPushButton::clicked, this, &MainWindow::amputation_slot );
connect(mAsthmaButton, &QPushButton::clicked, this, &MainWindow::asthma_slot );
// same way for others.
}
The slots that i have mentioned are just for example, connect it to the slot that you need it to be connected to.
UPDATE :
Here is a mini-implementation made using just the two buttons :
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QPushButton;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QPushButton *mAmputationButton;
QPushButton *mAsthmaButton;
private slots:
void on_pushButton_clicked();
};
MainWindow.cpp
#include "MainWindow.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
mAmputationButton( new QPushButton("Amputation" ) ),
mAsthmaButton( new QPushButton("Astham" ) )
{
setWindowTitle("First Aid");
QWidget *sampleWidget = new QWidget();
QVBoxLayout *lay = new QVBoxLayout();
lay->addWidget(mAmputationButton);
connect(mAmputationButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked );
connect(mAsthmaButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked );
lay->addWidget(mAsthmaButton);
sampleWidget->setLayout(lay);
setCentralWidget(sampleWidget);
}
MainWindow::~MainWindow(){}
void MainWindow::on_pushButton_clicked(){
QDialog *sampleDialog = new QDialog();
sampleDialog->setModal(true);
sampleDialog->show();
}
Note1 : In the slot on_pushbutton_clicked, I am just creating a new dialog and showing it. Just add your slot logic there and you will be good to go.
Note2 : It will be advisable to have all your connections in a single method such as Initialize Connections as already mentioned above.
Take this mini-implementation just as an example that you can work on top of rather than a copy-paste use of it.
I created a SQLite database called "test.db". It has only one table called "table1" which is not an empty table.
I tried to display "table1" in QTableView widget but the widget displayed nothing.
The database was connected and Qt Createor didn't detect any error. I don't understand why QTableView displayed nothing at all.
My Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QSqlDatabase>
#include <QSqlTableModel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase test = QSqlDatabase::addDatabase("QSQLITE");
test.setDatabaseName("test.db");
if (test.open())
{
ui->DB_Status->setText("DB is connected.");
}
else
{
ui->DB_Status->setText("DB is not connected.");
}
QSqlTableModel *model = new QSqlTableModel(this,test);
model->setTable("table1");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("Row1"));
model->setHeaderData(1, Qt::Horizontal, tr("Row2"));
ui->tableView->setModel(model);
ui->tableView->show();
}
I can run the program but QTableView widget diaplay nothing. Where is the problem ?
test.setDatabaseName("test.db"); // after this line open database connection.
test.open();
I'm starting with Qt. Previously I did with Java Swing, where I accomplished this by Card Layout. I have MainWindow, Login Widget and Dashboard Widget.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// set login screen on startup
QWidget *login = new Login(this);
setCentralWidget(login);
}
Login.cpp
#include "login.h"
#include "ui_login.h"
Login::Login(QWidget *parent) :
QWidget(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
}
Login::~Login()
{
delete ui;
}
void Login::on_loginButton_clicked()
{
// some logic
// here I want to create Dashboard widget in central widget
// but method setCentralWidget() can't be called from here
}
You want to use signals and slots to communicate between the windows. QDialog provides three important signals in particular: accepted(), rejected(), finished(int). So you'll want to do something like this:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// set login screen on startup
QWidget *login = new Login(this);
connect(login, SIGNAL(finished(int)), this, SLOT(loginFinished(int)));
setCentralWidget(login);
}
MainWindow::loginFinished(int reason)
{
Login *login = qobject_cast<Login*>(sender());
if (!login) {
qDebug() << "something bad happened!";
return;
}
login->deleteLater();
setCentralWidget(someOtherWidget);
}