Answer the question
In order to leave comments, you need to log in
`
Interaction QWidget`s. How to change the state of widget MainWindow from another class?
Good day.
The essence is this: the class of the main window (MainWindow) forms a window-> widgets, the structure (layer-> widget-> tree widget), the tree widget (QTreeWidget) is in another class (LeftView), then through the slot in the menu in the third class (PowerSelect ) I call a window in which the form, in the form by "Save" a new record is created, and then I need to update the tree and then everything stalled, I can not understand how to dynamically change and update the contents of the QTreeWidget widget from PowerSelect.
Thank you in advance for your attention! I would be glad at least a schematic description of how everything should interact
, that's what I have
MainWindow
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// init date base
datebase = new dataBase;
// main menu
MenuMain();
// left view
LeftView *leftview = new LeftView;
setCentralWidget(leftview);
setMinimumSize(750, 480);
resize(750, 480);
}
...................
LeftView::LeftView(QMainWindow *parent):QMainWindow(parent)
{
this->setMinimumSize(750, 350);
LeftView::LeftUpdate();
}
void LeftView::LeftUpdate()
{
LeftView::treeLeft();
LeftView::treeRight();
QGridLayout *layout = new QGridLayout;
layout->addWidget(treeWidget, 1, 0, 1, 5);
layout->addWidget(treeWidgetRight, 1, 5, 1, 8);
QWidget *widget = new QWidget();
setCentralWidget(widget);
widget->setLayout(layout);
}
void LeftView::treeLeft()
{
// tree widget
treeWidget = new QTreeWidget;
treeWidget->setHeaderLabel("Категории");
int intId = Global::userId;
QString id = QString::number(intId);
QSqlQuery query;
query.exec("SELECT * FROM `categories` WHERE `parent_id`='" + id + "' ");
while (query.next()) {
QTreeWidgetItem *topLevelItem=new QTreeWidgetItem(treeWidget);
treeWidget->addTopLevelItem(topLevelItem);
QString name = query.value("name").toString();
topLevelItem->setText(0, name);
//
QTreeWidgetItem *item = new QTreeWidgetItem(topLevelItem);
QTreeWidgetItem *item1 = new QTreeWidgetItem(topLevelItem);
//
item->setText(0,"Под итем");
item1->setText(0,"Под итемs");
item->setExpanded ( true );
}
}
...................
PowerSelect::PowerSelect(QWidget* pwgt)
: QDialog(pwgt, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
QWidget *widget1 = new QWidget();
widget1->setWindowTitle(tr("Управление списком"));
// inputs
m_ptxtNameCat = new QLineEdit;
m_ptxtDescriptionCat = new QLineEdit;
// m_ptxtPassword->setEchoMode(QLineEdit:: Password);
QLabel* plblNewCat = new QLabel("<b>Добавить категорию</b>");
QLabel* plblNewList = new QLabel("<b>Добавить лист паролей</b>");
QLabel* plblNameCat = new QLabel("&Название категории");
QLabel* plblDescriptionCat = new QLabel("&Описание категории");
plblNameCat->setBuddy(m_ptxtNameCat);
plblDescriptionCat->setBuddy(m_ptxtDescriptionCat);
QPushButton* pcmdAddCat = new QPushButton("&Сохранить");
connect(pcmdAddCat, SIGNAL(clicked()), SLOT(addCategory()));
//////////////////////////
m_ptxtSelectCst = new QComboBox;
QString itemSel = "hallo";
m_ptxtSelectCst->addItem(itemSel);
m_ptxtNameList = new QLineEdit;
QLabel* plblSelect = new QLabel("&Категории");
QLabel* plblName = new QLabel("&Название списка");
QLabel* br = new QLabel("");
plblSelect->setBuddy(m_ptxtSelectCst);
plblName->setBuddy(m_ptxtNameList);
QPushButton* pcmdAddList = new QPushButton("&Сохранить");
// connect(pcmdOk, SIGNAL(clicked()), SLOT(checkLogin()));
// connect(pcmdCancel, SIGNAL(clicked()), SLOT(closeApp()));
//Layout setup
QGridLayout* ptopLayout = new QGridLayout;
///
ptopLayout->addWidget(plblNewCat, 0, 0, 1, 1);
///
ptopLayout->addWidget(plblNameCat, 1, 0, 1, 0);
ptopLayout->addWidget(plblDescriptionCat, 2, 0, 1, 0);
ptopLayout->addWidget(m_ptxtNameCat, 1, 1, 1, 1);
ptopLayout->addWidget(m_ptxtDescriptionCat, 2, 1, 1, 1);
ptopLayout->addWidget(pcmdAddCat, 3, 1, 1, 1);
ptopLayout->addWidget(br, 4, 1, 1, 1);
///
ptopLayout->addWidget(plblNewList, 5, 0, 1, 1);
///
ptopLayout->addWidget(plblSelect, 6, 0, 1, 0);
ptopLayout->addWidget(plblName, 7, 0, 1, 0);
ptopLayout->addWidget(m_ptxtSelectCst, 6, 1, 1, 1);
ptopLayout->addWidget(m_ptxtNameList, 7, 1, 1, 1);
ptopLayout->addWidget(pcmdAddList, 8, 1, 1, 1);
widget1->setLayout(ptopLayout);
QWidget *widget2 = new QWidget();
QWidget *widget3 = new QWidget();
QTabWidget *tabWidget = new QTabWidget();
tabWidget->addTab(widget1, tr("Добавление"));
tabWidget->addTab(widget2, tr("Редактирование"));
tabWidget->addTab(widget3, tr("Удаление"));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(tabWidget);
setLayout(mainLayout);
setWindowTitle(tr("Управление списком"));
}
void PowerSelect::PowerSelectShow()
{
PowerSelect *frm = new PowerSelect();
frm->setWindowModality(Qt::ApplicationModal);
frm->show();
}
/**
* functions add category tree left
* @brief LeftView::addCategory
*/
void PowerSelect::addCategory()
{
QString name = m_ptxtNameCat->text().trimmed();
QString description = m_ptxtDescriptionCat->text().trimmed();
// QChar id = Global::userId.to;
QDate dateToday = QDate::currentDate();
QString dateTodayS = dateToday.toString("yyyy-MM-dd");
QTime timeToday = QTime::currentTime();
QString timeTodayS = timeToday.toString("hh:mm:ss");
int intId = Global::userId;
QString id = QString::number(intId);
if(name != ""){
QSqlQuery query;
query.exec("INSERT INTO `categories` ('name', 'parent_id', 'description', 'created_at', 'updated_at') VALUES ('" + name + "', '" + id + "', '" + description + "',"
" '" + dateTodayS + " " + timeTodayS + "', '" + dateTodayS + " " + timeTodayS + "');");
QMessageBox::information(this, "Ooops", query.lastError().text());
m_ptxtNameCat->setText("");
m_ptxtDescriptionCat->setText("");
// LeftView *left = new LeftView();
// left->LeftUpdate();
} else {
QMessageBox::information(this, "Ooops", "Заполните поле \"Название категории\"");
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question