#include "familytab.h"#include <QVBoxLayout>#include "mydedelegate.h"#include "creport.h"FamilyTab::FamilyTab(QWidget *parent) : QTabWidget(parent){ m_pAnketaModel = new QSqlRelationalTableModel(this); m_pAnketaModel->setTable("anketa"); m_pAnketaModel->setFilter("idperson > 0"); m_pAnketaModel->setSort(Anketa_fio,Qt::AscendingOrder); m_pAnketaModel->setHeaderData(Anketa_fio, Qt::Horizontal, tr("ФИО")); m_pAnketaModel->setHeaderData(Anketa_birthdate,Qt::Horizontal, tr("Дата рождения")); m_pAnketaModel->setHeaderData(Aketa_birthplace, Qt::Horizontal, tr("Место рождения")); m_pAnketaModel->setHeaderData(Anketa_adres, Qt::Horizontal,tr("Адрес")); m_pAnketaModel->setHeaderData(Anketa_nomerseria,Qt::Horizontal, tr("Номер/Серия")); m_pAnketaModel->setHeaderData(Anketa_characteristics, Qt::Horizontal, tr("Характеристика")); m_pAnketaModel->setHeaderData(Anketa_addingdate, Qt::Horizontal, tr("Время добавления")); m_pAnketaModel->select(); /* m_pAnketaView = new QTableView; m_pAnketaView->setModel(m_pAnketaModel); m_pAnketaView->setColumnHidden(Anketa_Id, true); m_pAnketaView->setSelectionMode(QAbstractItemView::SingleSelection); m_pAnketaView->setSelectionBehavior(QAbstractItemView::SelectRows); m_pAnketaView->resizeColumnsToContents();*/ m_pTrackRecordModel = new QSqlTableModel(this); m_pTrackRecordModel->setTable("workingtrack"); m_pTrackRecordModel->setHeaderData(workingplace, Qt::Horizontal, tr("Место работы")); m_pTrackRecordModel->setHeaderData(working_enteringdate, Qt::Horizontal, tr("Дата поступления")); m_pTrackRecordModel->setHeaderData(working_sakingdate, Qt::Horizontal, tr("Дата увольнения")); m_pTrackRecordModel->setHeaderData(working_sakingreason, Qt::Horizontal, tr("Причина увольнения")); m_pTrackRecordView = new QTableView; m_pTrackRecordView->setModel(m_pTrackRecordModel); //m_pTrackRecordView->setItemDelegate(new MyDEDelegate(working_enteringdate, this)); //m_pTrackRecordView->setItemDelegate(new MyDEDelegate(working_sakingdate, this)); m_pTrackRecordView->setSelectionMode(QAbstractItemView::SingleSelection); m_pTrackRecordView->setSelectionBehavior(QAbstractItemView::SelectRows); m_pTrackRecordView->resizeColumnsToContents(); AddTabs(); m_pLayout = new QGridLayout; m_pAddButton = new QPushButton(QObject::tr("Добавить")); m_pAddButton->setFocusPolicy(Qt::NoFocus); m_pEditButton = new QPushButton(QObject::tr("Редактировать")); m_pEditButton->setFocusPolicy(Qt::NoFocus); m_pDeleteButton = new QPushButton(QObject::tr("Удалить")); m_pDeleteButton->setFocusPolicy(Qt::NoFocus); m_pReportButton = new QPushButton(QObject::tr("Отчёт")); m_pReportButton->setFocusPolicy(Qt::NoFocus); m_pOKButton = new QPushButton(QObject::tr("OK")); m_pOKButton->setFocusPolicy(Qt::NoFocus); m_pQuitButton = new QPushButton(QObject::tr("Выход")); m_pQuitButton->setFocusPolicy(Qt::NoFocus); m_pButtonLayout = new QHBoxLayout; m_pButtonLayout->addWidget(m_pAddButton); m_pButtonLayout->addWidget(m_pEditButton); m_pButtonLayout->addWidget(m_pDeleteButton); m_pButtonLayout->addWidget(m_pReportButton); m_pButtonLayout->addWidget(m_pOKButton); m_pButtonLayout->addWidget(m_pQuitButton); m_pButtonLayout->addStretch(); // m_pSplitter = new QSplitter(Qt::Vertical); // m_pSplitter->addWidget(m_pAnketaView); // m_pSplitter->addWidget(m_pTrackRecordView); m_pMainLayout = new QVBoxLayout(this); m_pMainLayout->addWidget(m_pAnketaView); m_pMainLayout->addWidget(m_pTrackRecordView); m_pMainLayout->addLayout(m_pButtonLayout); currentFamilyChanged (QModelIndex()); connect(m_pAnketaView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(currentFamilyChanged(const QModelIndex &))); connect(m_pTrackRecordModel, SIGNAL(beforeInsert(QSqlRecord &)), this, SLOT(beforeInsertTrack(QSqlRecord &))); connect(m_pAddButton, SIGNAL(clicked()), this, SLOT(AddTrack())); connect(m_pQuitButton, SIGNAL(clicked()), this, SLOT(close())); connect(m_pOKButton, SIGNAL(clicked()), this, SLOT(ApplyOK())); connect(m_pDeleteButton, SIGNAL(clicked()), this, SLOT(deleteTrack())); connect(m_pEditButton, SIGNAL(clicked()), this, SLOT(EditTrack())); connect(m_pReportButton, SIGNAL(clicked()), this, SLOT(OpenReport()));}void FamilyTab::AddTabs(){ QSqlQuery query("SELECT fio FROM anketa WHERE idperson > 0"); query.exec(); int index = query.record().indexOf("fio"); QString sBookva; QSet <QString> set; QString sWord; while(query.next()) { QString sFio = query.value(index).toString(); sBookva = sFio[0]; set << sBookva; } foreach(QString value, set) { QVariant m_pVariant = value; sWord = m_pVariant.toString(); m_pAnketaView = new QTableView; m_pAnketaView->setModel(m_pAnketaModel); m_pAnketaView->setColumnHidden(Anketa_Id, true); m_pAnketaView->setSelectionMode(QAbstractItemView::SingleSelection); m_pAnketaView->setSelectionBehavior(QAbstractItemView::SelectRows); m_pAnketaView->resizeColumnsToContents(); addTab(m_pAnketaView, sWord); } }
C++ (Qt)QVBoxLayout * vl = new QVBoxLayout;QTabWidget * tab = new QTabWidget(this);m_pAddButton = new QPushButton(QObject::tr("Добавить"));//.........vl->addWidget(tab);vl->addWidget(m_pAddButton);//..........AddTabs();// здесь создаешь QTableView и добавляешь через addTab()
FamilyTab::FamilyTab(QWidget *parent) : QWidget(parent){ AddTabs(); m_pLayout = new QGridLayout; m_pAddButton = new QPushButton(QObject::tr("Добавить")); m_pAddButton->setFocusPolicy(Qt::NoFocus); m_pEditButton = new QPushButton(QObject::tr("Редактировать")); m_pEditButton->setFocusPolicy(Qt::NoFocus); m_pDeleteButton = new QPushButton(QObject::tr("Удалить")); m_pDeleteButton->setFocusPolicy(Qt::NoFocus); //и т.д. m_pSplitter = new QSplitter(Qt::Vertical); m_pSplitter->addWidget(m_pTabWidget); m_pSplitter->addWidget(m_pTrackRecordView); m_pMainLayout = new QVBoxLayout(this); //m_pMainLayout->addWidget(m_pSplitter); m_pMainLayout->addWidget(m_pSplitter); // m_pMainLayout->addWidget(m_pTrackRecordView); m_pMainLayout->addLayout(m_pButtonLayout);void FamilyTab::AddTabs(){ QSqlQuery query("SELECT fio FROM anketa WHERE idperson > 0 ORDER BY fio"); query.exec(); int index = query.record().indexOf("fio"); QString sBookva; QSet <QString> set; QString sWord; m_pTabWidget = new QTabWidget; while(query.next()) { QString sFio = query.value(index).toString(); sBookva = sFio[0]; set << sBookva; } foreach(QString value, set) { QVariant m_pVariant = value; sWord = m_pVariant.toString(); // QTableView* m_pAnketaView = static_cast<QTableView*>(currentWidget()); m_pAnketaModel = new QSqlRelationalTableModel(this); m_pAnketaModel->setTable("anketa"); m_pAnketaModel->setFilter("fio LIKE`" + sWord + "%`"); // + "AND idperson > 0" m_pAnketaModel->setSort(Anketa_fio,Qt::AscendingOrder); m_pAnketaModel->setHeaderData(Anketa_fio, Qt::Horizontal, tr("ФИО")); m_pAnketaModel->setHeaderData(Anketa_birthdate,Qt::Horizontal, tr("Дата рождения")); m_pAnketaModel->setHeaderData(Aketa_birthplace, Qt::Horizontal, tr("Место рождения")); m_pAnketaModel->setHeaderData(Anketa_adres, Qt::Horizontal,tr("Адрес")); m_pAnketaModel->setHeaderData(Anketa_nomerseria,Qt::Horizontal, tr("Номер/Серия")); m_pAnketaModel->setHeaderData(Anketa_characteristics, Qt::Horizontal, tr("Характеристика")); m_pAnketaModel->setHeaderData(Anketa_addingdate, Qt::Horizontal, tr("Время добавления")); m_pAnketaModel->select(); m_pAnketaView = new QTableView; m_pAnketaView->setModel(m_pAnketaModel); m_pAnketaView->setColumnHidden(Anketa_Id, true); m_pAnketaView->setSelectionMode(QAbstractItemView::SingleSelection); m_pAnketaView->setSelectionBehavior(QAbstractItemView::SelectRows); m_pAnketaView->resizeColumnsToContents(); m_pTrackRecordModel = new QSqlTableModel(this); m_pTrackRecordModel->setTable("workingtrack"); m_pTrackRecordModel->setHeaderData(workingplace, Qt::Horizontal, tr("Место работы")); m_pTrackRecordModel->setHeaderData(working_enteringdate, Qt::Horizontal, tr("Дата поступления")); m_pTrackRecordModel->setHeaderData(working_sakingdate, Qt::Horizontal, tr("Дата увольнения")); m_pTrackRecordModel->setHeaderData(working_sakingreason, Qt::Horizontal, tr("Причина увольнения")); m_pTrackRecordView = new QTableView; m_pTrackRecordView->setModel(m_pTrackRecordModel); //m_pTrackRecordView->setItemDelegate(new MyDEDelegate(working_enteringdate, this)); //m_pTrackRecordView->setItemDelegate(new MyDEDelegate(working_sakingdate, this)); m_pTrackRecordView->setSelectionMode(QAbstractItemView::SingleSelection); m_pTrackRecordView->setSelectionBehavior(QAbstractItemView::SelectRows); m_pTrackRecordView->resizeColumnsToContents(); currentFamilyChanged (QModelIndex()); connect(m_pAnketaView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(currentFamilyChanged(const QModelIndex &))); m_pTabWidget->addTab(m_pAnketaView, sWord); }}
void FamilyTab::currentFamilyChanged(const QModelIndex &index){ if (index.isValid()) { QSqlRecord m_precord = m_pAnketaModel->record(index.row()); int id = m_precord.value("idperson").toInt(); m_pTrackRecordModel->setFilter(QString("idperson = %1").arg(id)); } else { m_pTrackRecordModel->setFilter("idperson = -1"); } m_pTrackRecordModel->select();}