1
0
forked from BRT/arc
arc/views/MainWindow.cpp
slava 4d9889db46 Refactor UI and database logic for device management
Replaced `MyWindow` with `MainWindow` featuring dynamic device widgets. Introduced `DeviceInfo`, `DeviceDAO`, and improved database interaction. Added flow layout and refactored image handling for better performance.
2025-04-18 16:39:13 +07:00

64 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "MainWindow.h"
#include <QLabel>
#include <QScrollArea>
#include "DeviceInfo.h"
#include "DeviceWidget.h"
#include "dao/DeviceDAO.h"
#include "widget/FlowLayout.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
// QPushButton* btn = new QPushButton("Hello Qt6!", this);
// setCentralWidget(btn);
auto* scrollArea = new QScrollArea(this);
auto* contentWidget = new QWidget;
flowLayout = new FlowLayout(contentWidget);
contentWidget->setContentsMargins(0, 0, 0, 0); // Убираем отступы у контейнера
flowLayout->setContentsMargins(0, 0, 0, 0); // Убираем отступы у layout
flowLayout->setSpacing(0);
QVector<DeviceInfo> devices = DeviceDAO::getAllDevices();
for (const auto& device : devices) {
auto* deviceWidget = new DeviceWidget(device); // Создаем виджет, используя первый элемент из списка devices
deviceWidget->setFixedWidth(220);
deviceWidget->setFixedHeight(400);
deviceWidget->setContentsMargins(0, 0, 0, 0);
flowLayout->addWidget(deviceWidget);
}
resize(800, 600);
contentWidget->setLayout(flowLayout);
scrollArea->setWidget(contentWidget);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::loadDevices);
timer->start(1000); // каждые 1 сек
}
void MainWindow::loadDevices() const {
const auto devices = DeviceDAO::getAllDevices();
// Очистить старые виджеты
QLayoutItem* item;
while ((item = flowLayout->takeAt(0)) != nullptr) {
delete item->widget(); // Удаляем виджет
delete item; // Удаляем сам элемент из layout
}
// Добавляем новые виджеты для каждого устройства
for (const auto& device : devices) {
flowLayout->addWidget(new DeviceWidget(device));
}
}