1
0
forked from BRT/arc
arc/views/MainWindow.cpp
slava c1b8d0c23c Add menu navigation and cleanup main.cpp code
Introduced a navigation menu with actions "Окно 1" and "Окно 2" in `MainWindow`. Cleaned up commented-out code and adjusted formatting in `main.cpp` for clarity.
2025-04-22 10:18:52 +07:00

74 lines
2.3 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 <QMenuBar>
#include "db/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);
// Меню
QMenu* menu = menuBar()->addMenu("Навигация");
QAction* actionPage1 = new QAction("Окно 1", this);
QAction* actionPage2 = new QAction("Окно 2", this);
menu->addAction(actionPage1);
menu->addAction(actionPage2);
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));
}
}