Last active
November 27, 2021 23:53
-
-
Save tacchang001/f8b4287020d30b3b5d8f6fc96a639963 to your computer and use it in GitHub Desktop.
Qt5のQWidgetでsetFixedHeight()は使わず、自身に内包するウィジェットの高さを合計した高さに自動調整して欲しい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QDebug> | |
#include "AbstractPanel.h" | |
AbstractPanel::AbstractPanel(QWidget *parent) : QAbstractItemView(parent) | |
{ | |
} | |
QRect AbstractPanel::visualRect(const QModelIndex &index) const | |
{ | |
Q_UNUSED(index); | |
return QRect(0, 0, 300, 200); | |
} | |
void AbstractPanel::scrollTo(const QModelIndex &index, ScrollHint hint) | |
{ | |
Q_UNUSED(index); | |
Q_UNUSED(hint); | |
} | |
QModelIndex AbstractPanel::indexAt(const QPoint &point) const | |
{ | |
Q_UNUSED(point); | |
return QModelIndex(); | |
} | |
QModelIndex AbstractPanel::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) | |
{ | |
Q_UNUSED(cursorAction); | |
Q_UNUSED(modifiers); | |
return QModelIndex(); | |
} | |
int AbstractPanel::horizontalOffset() const | |
{ | |
return 0; | |
} | |
int AbstractPanel::verticalOffset() const | |
{ | |
return 0; | |
} | |
bool AbstractPanel::isIndexHidden(const QModelIndex &index) const | |
{ | |
Q_UNUSED(index) | |
return false; | |
} | |
void AbstractPanel::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) | |
{ | |
Q_UNUSED(rect); | |
Q_UNUSED(command); | |
} | |
QRegion AbstractPanel::visualRegionForSelection(const QItemSelection &selection) const | |
{ | |
Q_UNUSED(selection); | |
return QRegion(); | |
} | |
void AbstractPanel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) | |
{ | |
Q_UNUSED(bottomRight); | |
Q_UNUSED(roles); | |
//モデルからデータを取得し、表示内容を更新する. | |
updateView(topLeft, model()->data(topLeft)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <QAbstractItemView> | |
class AbstractPanel : public QAbstractItemView | |
{ | |
Q_OBJECT | |
public: | |
explicit AbstractPanel(QWidget *parent = nullptr); | |
QRect visualRect(const QModelIndex &index) const override; | |
void scrollTo(const QModelIndex &index, ScrollHint hint) override; | |
QModelIndex indexAt(const QPoint &point) const override; | |
virtual void updateView(const QModelIndex &index, const QVariant& data) = 0; | |
protected slots: | |
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) override; | |
protected: | |
QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override; | |
int horizontalOffset() const override; | |
int verticalOffset() const override; | |
bool isIndexHidden(const QModelIndex &index) const override; | |
void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) override; | |
QRegion visualRegionForSelection(const QItemSelection &selection) const override; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set(EXAMPLE_NAME groupbox03) | |
file(GLOB SOURCES *.cpp) | |
file(GLOB HEADERS *.h) | |
add_definitions(${Qt5Core_DEFINITIONS}) | |
add_definitions(${Qt5Widgets_DEFINITIONS}) | |
add_definitions(${Qt5Gui_DEFINITIONS}) | |
list(APPEND CMAKE_CXX_FLAGS "${Qt5Core_EXECUTABLE_COMPILE_FLAGS}") | |
list(APPEND CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}") | |
list(APPEND CMAKE_CXX_FLAGS "${Qt5Gui_EXECUTABLE_COMPILE_FLAGS}") | |
add_executable(${EXAMPLE_NAME} | |
${SOURCES} | |
${HEADERS} | |
) | |
target_include_directories(${EXAMPLE_NAME} PRIVATE | |
${Qt5Core_INCLUDES} | |
${Qt5Widgets_INCLUDES} | |
${Qt5Gui_INCLUDES} | |
) | |
target_link_libraries(${EXAMPLE_NAME} PRIVATE | |
${Qt5Core_LIBRARIES} | |
${Qt5Widgets_LIBRARIES} | |
${Qt5Gui_LIBRARIES} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QFile> | |
#include <QDebug> | |
#include "mainwindow.h" | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
QFile File("style.qss"); | |
if (File.open(QFile::ReadOnly)) | |
{ | |
QString StyleSheet = QLatin1String(File.readAll()); | |
a.setStyleSheet(StyleSheet); | |
} | |
else { | |
qWarning() << "style.qss not found."; | |
} | |
MainWindow w; | |
w.show(); | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QDebug> | |
#include <QVBoxLayout> | |
#include <QTableWidget> | |
#include <QHeaderView> | |
#include <QPushButton> | |
#include <QHBoxLayout> | |
#include <QLabel> | |
#include <QComboBox> | |
#include <QTableView> | |
#include <QStandardItemModel> | |
#include <QSizePolicy> | |
#include <QGroupBox> | |
#include "mainwindow.h" | |
#include "SubPanel.h" | |
MainWindow::MainWindow(QWidget *parent) : | |
QWidget(parent) | |
{ | |
auto mainLayout = new QVBoxLayout(this); | |
{ | |
auto layoutTop = new QHBoxLayout; | |
{ | |
auto button = new QPushButton("top", this); | |
layoutTop->addWidget(button); | |
auto comboBox = new QComboBox(this); | |
layoutTop->addWidget(comboBox); | |
} | |
mainLayout->addLayout(layoutTop); | |
auto layoutMiddle = new QHBoxLayout; | |
{ | |
auto layoutLeft = new QVBoxLayout; | |
{ | |
auto button = new QPushButton("middle left", this); | |
layoutLeft->addWidget(button); | |
auto group1 = new QGroupBox("GroupBox 1"); | |
{ | |
auto layout = new QVBoxLayout; | |
{ | |
auto button1 = new QPushButton("middle left groupbox1", this); | |
layout->addWidget(button1); | |
} | |
group1->setLayout(layout); | |
} | |
layoutLeft->addWidget(group1); | |
auto group2 = new QGroupBox("GroupBox 2"); | |
{ | |
auto layout = new QVBoxLayout; | |
{ | |
auto button1 = new QPushButton("middle left groupbox2", this); | |
layout->addWidget(button1); | |
} | |
group2->setLayout(layout); | |
} | |
layoutLeft->addWidget(group2); | |
} | |
layoutMiddle->addLayout(layoutLeft); | |
auto layoutRight = new QVBoxLayout; | |
{ | |
auto button = new QPushButton("middle right", this); | |
layoutRight->addWidget(button); | |
} | |
layoutMiddle->addLayout(layoutRight); | |
} | |
mainLayout->addLayout(layoutMiddle); | |
auto layoutBottom = new QVBoxLayout; | |
{ | |
/* | |
* QFrame派生であっても、妙な間を作らずぴったりとした高さにしたい | |
*/ | |
#if defined(NOT_AS_EXPECTED) | |
auto sub = new SubPanel(this); | |
layoutBottom->addWidget(sub, 0, Qt::AlignTop); | |
layoutBottom->setSizeConstraint(QLayout::SetFixedSize); | |
#else | |
auto button = new QPushButton("middle bottom", this); | |
layoutBottom->addWidget(button); | |
auto table = new QTableWidget(this); | |
{ | |
table->setStyleSheet("background-color: Blue;"); | |
table->setColumnCount(3); | |
table->setRowCount(3); | |
table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
auto h = table->horizontalHeader(); | |
h->setSectionResizeMode(0, QHeaderView::Stretch); | |
h->setSectionResizeMode(1, QHeaderView::Stretch); | |
h->setSectionResizeMode(2, QHeaderView::Stretch); | |
table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); | |
table->resizeRowsToContents(); | |
int nRowHeight = table->rowHeight(0); | |
int nTableHeight = | |
(table->rowCount() * nRowHeight) + | |
table->horizontalHeader()->height() + | |
2 * table->frameWidth(); | |
table->setMinimumHeight(nTableHeight); | |
table->setMaximumHeight(nTableHeight); | |
} | |
#endif | |
} | |
mainLayout->addLayout(layoutBottom); | |
setLayout(mainLayout); | |
mainLayout->addStretch(); | |
} | |
adjustSize(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <QWidget> | |
class MainWindow : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
explicit MainWindow(QWidget *parent = 0); | |
~MainWindow() override = default; | |
private: | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QDebug> | |
#include <QWidget> | |
#include <QDebug> | |
#include <QGroupBox> | |
#include <QLabel> | |
#include <QComboBox> | |
#include <QTableWidget> | |
#include <QTableWidgetItem> | |
#include <QHeaderView> | |
#include "SubPanel.h" | |
SubPanel::SubPanel(QWidget *parent) | |
: AbstractPanel(parent) | |
{ | |
auto layout = new QVBoxLayout(this); | |
{ | |
auto button = new QPushButton("middle bottom", this); | |
layout->addWidget(button, 1, Qt::AlignTop); | |
auto table = new QTableWidget(this); | |
{ | |
table->setStyleSheet("background-color: Blue;"); | |
table->setColumnCount(3); | |
table->setRowCount(3); | |
table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
auto h = table->horizontalHeader(); | |
h->setSectionResizeMode(0, QHeaderView::Stretch); | |
h->setSectionResizeMode(1, QHeaderView::Stretch); | |
h->setSectionResizeMode(2, QHeaderView::Stretch); | |
table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); | |
table->resizeRowsToContents(); | |
int nRowHeight = table->rowHeight(0); | |
int nTableHeight = | |
(table->rowCount() * nRowHeight) + | |
table->horizontalHeader()->height() + | |
2 * table->frameWidth(); | |
table->setMinimumHeight(nTableHeight); | |
table->setMaximumHeight(nTableHeight); | |
} | |
layout->addWidget(table, 1, Qt::AlignTop); | |
} | |
setLayout(layout); // 主レイアウトに設定する | |
// layout->setSizeConstraint(QLayout::SetFixedSize); | |
// setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); | |
// setFixedHeight(100); | |
} | |
/* | |
* | |
*/ | |
SubPanel::~SubPanel() | |
= default; | |
void SubPanel::initialize() | |
{ | |
} | |
void SubPanel::updateView(const QModelIndex &index, const QVariant &data) | |
{ | |
switch (index.row()) { | |
case 0: | |
switch (index.column()) { | |
case 0: { | |
break; | |
} | |
default: | |
break; | |
} | |
break; | |
default: | |
break; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <QObject> | |
#include <QCloseEvent> | |
#include <QLabel> | |
#include <QPushButton> | |
#include <QHBoxLayout> | |
#include "AbstractPanel.h" | |
class SubPanel : public AbstractPanel | |
{ | |
Q_OBJECT | |
friend class TestQzmPanel12; | |
public: | |
explicit SubPanel(QWidget *parent = nullptr); | |
~SubPanel() override; | |
void initialize(); | |
void updateView(const QModelIndex &index, const QVariant &data) override; | |
private slots: | |
private: | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment