How can I include multiple classes together?
-
btnitemdelegate.h file;
#ifndef BTNITEMDELEGATE_H #define BTNITEMDELEGATE_H #include <QItemDelegate> #include <QObject> #include <QModelIndex> #include <QStyleOptionButton> #include <QStyleOptionViewItem> #include <QPainter> #include <QItemDelegate> #include <QSqlQueryModel> #include <QPushButton> #include <QApplication> #include <QMouseEvent> #include <QDialog> #include <QMessageBox> #include <QEvent> class btnItemDelegate : public QItemDelegate { Q_OBJECT public: btnItemDelegate(const QString key,QObject *parent = nullptr); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &mindex); signals: void clickedDeleteBtnSignal(const QModelIndex &mindex); void clickedUpdateBtnSignal(const QModelIndex &mindex); private: QString _key=""; QWidget *editor = nullptr; }; #endif // BTNITEMDELEGATE_H
btnitemdelegate.cpp file;
#include "btnitemdelegate.h" #include "mainpage.h" #include "cstmbtnstyloption.h" #include "update.h" #include <QLabel> #include <QHBoxLayout> #include <QStyledItemDelegate> #include <QMessageBox> btnItemDelegate::btnItemDelegate(const QString key,QObject *parent) : QItemDelegate{parent},_key(key) { //Update *update = new Update(); //connect(this, &btnItemDelegate::clickedUpdateBtnSignal, this, &Update::updateBtnClickedSlot); } QWidget *btnItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QWidget *editor = new QWidget(parent); QHBoxLayout *layout = new QHBoxLayout(editor); layout->setContentsMargins(0, 0, 0, 0); QPushButton *deleteBtn = new QPushButton("Sil", editor); QPushButton *updateBtn = new QPushButton("Güncelle", editor); layout->addWidget(deleteBtn); layout->addWidget(updateBtn); editor->setLayout(layout); return editor; } void btnItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if(_key=="delete") { CstmBtnStylOption deleteButtonOption; // QStyleOptionButton deleteButtonOption; QRect deleteRect = option.rect;//hücrenin dikdörtgeni int x,y,w,h; x = deleteRect.left() + deleteRect.width() - 90;// X coordinate y = deleteRect.top(); w = 80; h = 30; deleteButtonOption.rect = QRect(x,y,w,h); // Sil düğmesi konumu deleteButtonOption.features = QStyleOptionButton::None; // Düğme özellikleri deleteButtonOption.state = QStyle::State_Enabled; // Düğme durumu QIcon icon(":/icon/icons/deletebtn.png"); deleteButtonOption.icon = icon; deleteButtonOption.iconSize = QSize(25,25); deleteButtonOption.palette.setColor(QPalette::Button, deleteButtonOption.getBtnColor()); QApplication::style()->drawControl( QStyle::CE_PushButton, &deleteButtonOption, painter); } else if(_key=="guncelle") { CstmBtnStylOption updateButtonOption; //QStyleOptionButton updateButtonOption; QRect updateRect = option.rect;//hücrenin dikdörtgeni int x2,y2,w2,h2; x2 = updateRect.left() + updateRect.width() - 90;// X coordinate y2 = updateRect.top(); w2 = 80; h2 = 30; updateButtonOption.rect = QRect(x2,y2,w2,h2); updateButtonOption.features = QStyleOptionButton::None; //Düğme özellikleri updateButtonOption.state = QStyle::State_Enabled; //Düğme durumu QIcon icon2(":/icon/icons/updatebutton.png"); updateButtonOption.icon = icon2; updateButtonOption.iconSize = QSize(25,25); updateButtonOption.palette.setColor(QPalette::Button, updateButtonOption.getBtnColor()); QApplication::style()->drawControl( QStyle::CE_PushButton, &updateButtonOption, painter); } else{ qDebug()<<"tanimsiz key"; } } /*void btnItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { }*/ /*void btnItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { }*/ void btnItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } bool btnItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &mindex) { Q_UNUSED(model) Q_UNUSED(option) Q_UNUSED(mindex) if (_key == "guncelle" && event->type() == QEvent::MouseButtonRelease) { emit clickedUpdateBtnSignal(mindex); // Güncelle düğmesi tıklama sinyali emit clickedDeleteBtnSignal(mindex); qDebug()<<"event update sinyal ok"; } //return true; return QItemDelegate::editorEvent(event, model, option, mindex); //default /*CstmBtnStylOption updateButtonOption; updateButtonOption.rect = option.rect; if (event->type() == QEvent::MouseButtonPress) { //mouse tıklaması updateButtonOption.state |= QStyle::State_Sunken; } if (event->type() == QEvent::MouseButtonRelease) { //mouse tıklamasını bırakınca updateButtonOption.state &= ~QStyle::State_Sunken; }*/ //buton rengi //updateButtonOption.palette.setColor(QPalette::Button, Qt::blue); }
mainpage.h file;
#ifndef MAINPAGE_H #define MAINPAGE_H #include "giriswidget.h" #include "btnitemdelegate.h" #include "addpage.h" #include "detailpage.h" #include "update.h" #include <QDialog> #include <QTableView> #include <QSql> #include <QSqlDatabase> #include <QSqlQueryModel> #include <QSqlQuery> #include <QStandardItemModel> #include <QPushButton> #include <QModelIndex> namespace Ui { class MainPage; } class MainPage : public QDialog { Q_OBJECT public: explicit MainPage(QWidget *parent = nullptr); ~MainPage(); void connectDb(); void addBtn_Page_Clicked(); void onStatus(); void detailBtn_Page_Clicked(); void clickedLogOutBtn_Clicked(); void updateTableView(); void myview_stil_layout(); public slots: void updateBtnClickedSlot(const QModelIndex &mindex); void deleteBtnClickedSlot(const QModelIndex &mindex); //int sevkHesaplama(); public: Ui::MainPage *ui; AddPage *add; DetailPage *detail; QSqlDatabase mydb; QSqlQueryModel *queryModel; QSqlQueryModel *queryModelDelete; QStandardItemModel *itemModelBtn; QPushButton *deleteBtn; QPushButton *updateBtn; const int ID={}; }; #endif // MAINPAGE_H
mainpage.cpp file;
#include "mainpage.h" #include "ui_mainpage.h" #include "giriswidget.h" #include "btnitemdelegate.h" #include "addpage.h" #include "detailpage.h" #include "update.h" #include <QMessageBox> #include <QDebug> #include <QSqlError> #include <QHeaderView> #include <QSqlQueryModel> #include <QStringList> #include <QWidget> #include <QModelIndex> #include <QObject> MainPage::MainPage(QWidget *parent) : QDialog(parent), ui(new Ui::MainPage) { ui->setupUi(this); connectDb(); onStatus(); updateTableView(); connect(ui->logOutBtn,&QPushButton::clicked,this,&MainPage::clickedLogOutBtn_Clicked); connect(ui->addBtn,&QPushButton::clicked,this,&MainPage::addBtn_Page_Clicked); connect(ui->detailBtn,&QPushButton::clicked,this,&MainPage::detailBtn_Page_Clicked); btnItemDelegate *btnDlgte = new btnItemDelegate("delete",ui->myView); btnItemDelegate *btnUpdate = new btnItemDelegate("guncelle",ui->myView); connect(btnDlgte, &btnItemDelegate::clickedDeleteBtnSignal, this, &MainPage::deleteBtnClickedSlot); connect(btnUpdate, &btnItemDelegate::clickedUpdateBtnSignal,this, &MainPage::updateBtnClickedSlot); //updatePage queryModel->insertColumn(queryModel->columnCount()); queryModel->setHeaderData(queryModel->columnCount()-1, Qt::Horizontal, tr("Sil")); ui->myView->setItemDelegateForColumn(queryModel->columnCount()-1,btnDlgte); //ok queryModel->insertColumn(queryModel->columnCount()); queryModel->setHeaderData(queryModel->columnCount()-1, Qt::Horizontal, tr("Güncelle")); ui->myView->setItemDelegateForColumn(queryModel->columnCount()-1,btnUpdate); //ok ui->myView->setSelectionBehavior(QAbstractItemView::SelectItems); ui->myView->setModel(queryModel); } void MainPage::connectDb() { mydb = QSqlDatabase::addDatabase("QMYSQL"); mydb.setHostName("localhost"); mydb.setDatabaseName("krn_iplikler"); mydb.setUserName("root"); mydb.setPassword("root"); mydb.setPort(3306); mydb.open(); if (mydb.isOpen()) { qDebug()<<"db connection is succesfully"; } else { QMessageBox::information(this,"Connection Failed.!","Database connection failed!!!!",QMessageBox::Cancel); qDebug()<<mydb.lastError().text(); } } void MainPage::addBtn_Page_Clicked() { add->setWindowTitle("Add Page"); add->setWindowIcon(QIcon(":/icon/icons/add.png")); add->setWindowFlags( Qt::WindowMinimizeButtonHint); add->setModal(true); add->adjustSize(); add->setFixedSize(1000,150); add->show(); } void MainPage::onStatus() { add = new AddPage(); detail = new DetailPage(); } void MainPage::detailBtn_Page_Clicked() { detail->setWindowTitle("Details Page"); detail->setWindowIcon(QIcon(":/icon/icons/detail.png")); detail->setWindowFlags( Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); detail->setModal(true); detail->adjustSize(); detail->setFixedSize(600,400); detail->show(); } void MainPage::clickedLogOutBtn_Clicked() { this->close(); GirisWidget *giris = new GirisWidget(); giris->setWindowTitle("Login Page"); giris->adjustSize(); giris->setFixedSize(450,350); giris->setWindowIcon(QIcon(":/icon/icons/login.png")); giris->setWindowFlags( Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); giris->show(); } void MainPage::updateTableView() { queryModel = new QSqlQueryModel(); queryModel->setQuery("SELECT * FROM iplikler",mydb); ui->myView->setModel(queryModel); myview_stil_layout(); } void MainPage::myview_stil_layout() { if (!queryModel) { return; } QStringList headernames; headernames <<"Sıra No"<<"Boyanan Miktar"<<"Cari Kodu"<<"Cari Adı"<<"Depodan Rezerv" <<"İplik Kodu"<<"İplik Adı"<<"Miktar"<<"Onay Zamanı"<<"Özel Notu"<<"Renk Kodu"<<"Renk Adı"<<"Sevk Edilecek Miktar"<<"Sevk Miktarı"; for (int i = 0; i < headernames.size(); ++i) { queryModel->setHeaderData(i, Qt::Horizontal, headernames[i]); } ui->myView->setStyleSheet("QHeaderView::section { background-color: lightblue ; color: black; font-weight: bold;}"); ui->myView->setModel(queryModel); ui->myView->verticalHeader()->setVisible(false); QHeaderView *horizontalHeader = ui->myView->horizontalHeader(); horizontalHeader->setResizeContentsPrecision(QHeaderView::ResizeToContents); } void MainPage::updateBtnClickedSlot(const QModelIndex &mindex) { if (mindex.isValid()) { int rowIndex = mindex.row(); qDebug() << "Güncelle butonuna tıklandı. Satır indeksi: " << rowIndex; auto itemID = queryModel->data(queryModel->index(rowIndex, 0), Qt::DisplayRole).toInt(); Update *update = new Update(itemID); //id ve model i update e iletme connect(update , &Update::verilerGuncellendi,this,[=](){ qDebug()<<"tablo guncellendi"; queryModel->setQuery("SELECT * FROM iplikler",mydb); queryModel->insertColumn(queryModel->columnCount()); queryModel->setHeaderData(queryModel->columnCount()-1, Qt::Horizontal, tr("Sil")); queryModel->insertColumn(queryModel->columnCount()); queryModel->setHeaderData(queryModel->columnCount()-1, Qt::Horizontal, tr("Güncelle")); ui->myView->setModel(queryModel); }); update->setWindowTitle("Update Page"); update->setWindowIcon(QIcon(":/icon/icons/updatepage.png")); update->setWindowFlags( Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); update->setModal(true); update->setFixedSize(1000,150); update->show(); }else { qDebug()<<"error index"; } } void MainPage::deleteBtnClickedSlot(const QModelIndex &mindex) { int rowIndex = mindex.row(); QModelIndex modelIndex = queryModel->index(rowIndex,0); int ID = queryModel->data(modelIndex,Qt::DisplayRole).toInt(); QSqlQuery deleteQuery; deleteQuery.prepare("DELETE FROM iplikler WHERE ID = :secilenID "); deleteQuery.bindValue(":secilenID",ID); if (deleteQuery.exec()) { qDebug()<<"veri silindi"; queryModel->setQuery("SELECT * FROM iplikler",mydb); queryModel->insertColumn(queryModel->columnCount()); queryModel->setHeaderData(queryModel->columnCount()-1, Qt::Horizontal, tr("Sil")); queryModel->insertColumn(queryModel->columnCount()); queryModel->setHeaderData(queryModel->columnCount()-1, Qt::Horizontal, tr("Güncelle")); ui->myView->setModel(queryModel); } else{ qDebug() << "Satır silinirken hata oluştu:" << deleteQuery.lastError().text(); } } /*int MainPage::sevkHesaplama() { }*/ MainPage::~MainPage() { delete ui; }
update.h file;
#ifndef UPDATE_H #define UPDATE_H #include "mainpage.h" #include <QDialog> #include <QSqlQueryModel> #include <QMessageBox> #include <QTableView> #include <QSqlDatabase> namespace Ui { class Update; } class Update : public QDialog { Q_OBJECT public: explicit Update(int itemID, QWidget *parent = nullptr); ~Update(); void updateBtnClicked(); void degerleriGetir(); public slots: void updateBtnClickedSlot(const QModelIndex &mindex); signals: void verilerGuncellendi(); private: Ui::Update *ui; QSqlQueryModel *queryModel; QMessageBox msg; QSqlDatabase *mydb; QTableView *myView; int m_ID; }; #endif // UPDATE_H
update.cpp file;
#include "update.h" #include "ui_update.h" #include "mainpage.h" #include <QAbstractItemModel> #include <QStyleOptionViewItem> #include <QDebug> #include <QSqlError> Update::Update(int itemID, QWidget *parent) : QDialog(parent),m_ID(itemID), ui(new Ui::Update) { ui->setupUi(this); connect(ui->guncelleBtn,&QPushButton::clicked,this,&Update::updateBtnClicked); degerleriGetir(); } Update::~Update() { delete ui; } void Update::updateBtnClickedSlot(const QModelIndex &mindex) { } void Update::updateBtnClicked() //2.aşama guncelleme // where sartı olarak m_ID { QString _iplikkod = ui->iplikkodLnedit->text(); QString _iplikname = ui->ipliknameLnedit->text(); QString _renkkod = ui->renkkodLnedit->text(); QString _renkname = ui->renknameLnedit->text(); QString _carikod = ui->carikodLnedit->text(); QString _cariname = ui->carinameLnedit->text(); QString _miktar = ui->miktarLnedit->text(); QString _boyamamiktar = ui->bmiktarLnedit->text(); QString _deporezerv = ui->drezervLnedit->text(); QString _ozelnot = ui->ozelNot->text(); QSqlQuery updateQuery(QSqlDatabase::database()); updateQuery.prepare("UPDATE iplikler SET iplikkod= :iplikkod, iplikname= :iplikname, " "renkkod= :renkkod, renkname= :renkname, carikod=:carikod, cariname=:cariname, miktar=:miktar, boyamamiktar=:boyamamiktar, deporezerv=:deporezerv ,ozelnot=:ozelnot " " where ID=:ID"); updateQuery.bindValue(":ID",m_ID); updateQuery.bindValue(":iplikkod",_iplikkod); updateQuery.bindValue(":iplikname",_iplikname); updateQuery.bindValue(":renkkod",_renkkod); updateQuery.bindValue(":renkname",_renkname); updateQuery.bindValue(":carikod",_carikod); updateQuery.bindValue(":cariname",_cariname); updateQuery.bindValue(":miktar",_miktar); updateQuery.bindValue(":boyamamiktar",_boyamamiktar); updateQuery.bindValue(":deporezerv",_deporezerv); updateQuery.bindValue(":ozelnot",_ozelnot); if (updateQuery.exec()) { qDebug()<<"updateQuery success"; msg.information(this,"Updated Success !!!","Database is Updated Successfully !!!"); //trigger tableview verilerGuncellendi(); //sinyal yayıldı }else{ msg.warning(this,"Updated Failed !!!","Database is Not Updated !!!"); qDebug()<<updateQuery.lastError().text(); } } void Update::degerleriGetir() //verileri line editte gösterme { QSqlQuery degerler(QSqlDatabase::database()); degerler.prepare("Select * from iplikler where ID=:ID"); degerler.bindValue(":ID",m_ID); if (degerler.exec()) { while (degerler.next()) { QString iplikKod = degerler.value("iplikkod").toString(); ui->iplikkodLnedit->setText(iplikKod); } }else{ msg.warning(this,"Degerler query failed !!!","Veriler update formuna getirilemedi!"); qDebug()<<degerler.lastError().text(); } }
These are the codes I wrote; I have classes named btnitemdelegate, mainpage and update. The signal goes from the btnitemdelegate class to the mainpage class with a parameter. I send a signal from the mainpage class to the update class with a parameter. I want you to do this here; How can I include the .h files of these classes with each other? I also want to send a signal from the update class to the mainpage class again. How can I do that? What I really want is; Learning how to include .h files with each other?
-
@yy_pc_programmer
Since mainpage is (presumably) your main page class, do not includemainpage.h
into any other.h
/.cpp
files. Only include it inmainpage.cpp
. Other classes should not need to know about or depend on mainpage.If you impose that rule on yourself it should become clearer/simpler.
-
If there is a signal which needs to go from mainpage to update, just write the slot in
update.h
and do theconnect()
from mainpage's own signal to that. -
If there is a signal which needs to go from update to mainpage, put the signal in
update.h
. Again, do theconnect()
from mainpage, this time to its own slot.
-
-
@yy_pc_programmer
It is certainly necessary to useconnect()
:connect(signallingObject, &SignallingClass::signal, slotObject, &SlotClass::slot);
So the place where you do this needs to have access to/know about the signalling object and the slot object. And so will have to include header files for both the signaller and the slotter.
When in doubt: connections are usually best made where the slot object is. Signallers really should not care, and do not need to know, about what slot objects might or might not be connected to their signals. A
connect()
usually belongs in either the slot object, or somewhere outside which knows about the slot object. Note how in your case it is (only) mainpage which knows about both signal and slot and so can make the connection. -
Y yy_pc_programmer has marked this topic as solved on