From 3d9a85d6c1d7a5fa84195e213bf4ec14b4f8cb08 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 29 Jul 2019 17:34:46 +0200 Subject: [PATCH] list box for changinf tests added --- playground/grids/GridWidgets.cpp | 7 +- playground/grids/TestBox.cpp | 74 +++++++++++------ playground/grids/TestBox.h | 3 + playground/grids/main.cpp | 137 ++++++++++++++++++++++++------- 4 files changed, 161 insertions(+), 60 deletions(-) diff --git a/playground/grids/GridWidgets.cpp b/playground/grids/GridWidgets.cpp index 2dc071c2..cde6c034 100644 --- a/playground/grids/GridWidgets.cpp +++ b/playground/grids/GridWidgets.cpp @@ -21,6 +21,8 @@ namespace setPalette( QColor( colorName.constData() ) ); setAutoFillBackground( true ); + + setAttribute( Qt::WA_LayoutUsesWidgetRect, true ); } void setPreferredWidth( qreal width ) @@ -72,8 +74,9 @@ GridWidgets::~GridWidgets() void GridWidgets::insert( const QByteArray& colorName, int row, int column, int rowSpan, int columnSpan ) { - m_layout->addWidget( new Rectangle( colorName ), - row, column, rowSpan, columnSpan ); + auto rectangle = new Rectangle( colorName ); + m_layout->addWidget( rectangle, row, column, rowSpan, columnSpan ); + rectangle->show(); } void GridWidgets::setSpacing( Qt::Orientations orientations, int spacing ) diff --git a/playground/grids/TestBox.cpp b/playground/grids/TestBox.cpp index d8f6b555..0c9bae61 100644 --- a/playground/grids/TestBox.cpp +++ b/playground/grids/TestBox.cpp @@ -12,29 +12,45 @@ TestBox::TestBox( QWidget* parent ) : QWidget( parent ) + , m_grids {} { setPalette( QColor( "Lavender" ) ); setAutoFillBackground( true ); setContentsMargins( QMargins( 10, 10, 10, 10 ) ); - m_grids[Skinny] = new GridSkinny( this ); - m_labels[Skinny] = new QLabel( "Skinny", this ); - - m_grids[Widgets] = new GridWidgets( this ); - m_labels[Widgets] = new QLabel( "Widget", this ); - - m_grids[Graphics] = new GridGraphics( this ); - m_labels[Graphics] = new QLabel( "Graphic", this ); - - m_grids[Quick] = new GridQuick( this ); - m_labels[Quick] = new QLabel( "Quick", this ); + for ( int i = 0; i < GridCount; i++ ) + m_labels[i] = new QLabel( this ); } TestBox::~TestBox() { } +void TestBox::reset() +{ + setColumns( 2 ); + + for ( auto grid : m_grids ) + delete grid; + + m_grids[Skinny] = new GridSkinny( this ); + m_grids[Widgets] = new GridWidgets( this ); + m_grids[Graphics] = new GridGraphics( this ); + m_grids[Quick] = new GridQuick( this ); + + for ( int i = 0; i < GridCount; i++ ) + { + m_grids[i]->show(); + + m_labels[i]->show(); + m_labels[i]->raise(); + m_labels[i]->setText( QString() ); + } + + updateGeometry(); +} + void TestBox::enableGrid( int index, bool on ) { m_grids[index]->setVisible( on ); @@ -161,7 +177,6 @@ void TestBox::setVisibleAt( int index, bool on ) accessor->setVisibleAt( index, on ); } } - } QSize TestBox::preferredSize() const @@ -171,21 +186,8 @@ QSize TestBox::preferredSize() const bool TestBox::event( QEvent* event ) { - if ( event->type() == QEvent::Polish ) - { - for ( int i = 0; i < GridCount; i++ ) - { - auto accessor = dynamic_cast< GridAccessor* >( m_grids[i] ); - auto label = m_labels[i]; - - const auto hint = accessor->preferredSize(); - - QString text = label->text(); - text += QString( ": ( %1x%2 )" ).arg( hint.width() ).arg( hint.height() ); - - label->setText( text ); - } - } + if ( event->type() == QEvent::LayoutRequest ) + layoutGrids(); return QWidget::event( event ); } @@ -195,6 +197,24 @@ void TestBox::resizeEvent( QResizeEvent* ) layoutGrids(); } +void TestBox::updateLabels() +{ + const QString texts[ GridCount ] = { "Skinny", "Widgets", "Graphics", "Quick" }; + + for ( int i = 0; i < GridCount; i++ ) + { + auto accessor = dynamic_cast< GridAccessor* >( m_grids[i] ); + auto label = m_labels[i]; + + const auto hint = accessor->preferredSize(); + + QString text = texts[i]; + text += QString( ": ( %1x%2 )" ).arg( hint.width() ).arg( hint.height() ); + + label->setText( text ); + } +} + void TestBox::layoutGrids() { /* diff --git a/playground/grids/TestBox.h b/playground/grids/TestBox.h index c7a9cd79..025787d2 100644 --- a/playground/grids/TestBox.h +++ b/playground/grids/TestBox.h @@ -27,6 +27,8 @@ class TestBox : public QWidget, public GridAccessor TestBox( QWidget* parent = nullptr ); ~TestBox() override; + void reset(); + void setColumns( int ); void enableGrid( int, bool on ); @@ -50,6 +52,7 @@ class TestBox : public QWidget, public GridAccessor protected: bool event( QEvent* ) override; void resizeEvent( QResizeEvent* ) override; + void updateLabels(); private: void layoutGrids(); diff --git a/playground/grids/main.cpp b/playground/grids/main.cpp index 36fb0659..b68daedb 100644 --- a/playground/grids/main.cpp +++ b/playground/grids/main.cpp @@ -7,31 +7,44 @@ #include #include +#include #include -class MainBox : public TestBox +#include +#include + +class Box : public TestBox { public: - MainBox( int id ) + Box( QWidget* parent = nullptr ) + : TestBox( parent ) { - using Testcase = void ( MainBox::* )(); - - const Testcase tests[] = + m_tests = { - &MainBox::test0, &MainBox::test1, &MainBox::test2, - &MainBox::test3, &MainBox::test4, &MainBox::test5, - &MainBox::test6, &MainBox::test7, &MainBox::test8, - &MainBox::test9 + &Box::test0, &Box::test1, &Box::test2, + &Box::test3, &Box::test4, &Box::test5, + &Box::test6, &Box::test7, &Box::test8, + &Box::test9, &Box::test10 }; - - const int count = static_cast< int >( sizeof( tests ) / sizeof( tests[0] ) ); - - if ( id < 0 || id >= count ) - id = 0; - - ( this->*tests[id] )(); } + void showTest( int id ) + { + reset(); + + id = qBound( 0, id, count() - 1 ); + m_tests[id]( *this ); + + updateLabels(); + } + + int count() const + { + return m_tests.size(); + } + + private: + void insert( const char* colorName, int row, int column ) { insert( colorName, row, column, 1, 1 ); @@ -39,8 +52,6 @@ class MainBox : public TestBox using TestBox::insert; - private: - void test0(); void test1(); void test2(); @@ -51,9 +62,12 @@ class MainBox : public TestBox void test7(); void test8(); void test9(); + void test10(); + + std::vector< std::function< void( Box& ) > > m_tests; }; -void MainBox::test0() +void Box::test0() { // something, that works with all layouts @@ -64,7 +78,7 @@ void MainBox::test0() insert( "NavajoWhite", 3, 0, 1, 3 ); } -void MainBox::test1() +void Box::test1() { /* Graphics layout adds the extra space to the stretchables @@ -91,7 +105,7 @@ void MainBox::test1() setColumnSizeHint( 1, Qt::MinimumSize, 100 ); } -void MainBox::test2() +void Box::test2() { setColumns( 1 ); @@ -109,7 +123,7 @@ void MainBox::test2() setMinimumWidthAt( 2, 100 ); } -void MainBox::test3() +void Box::test3() { /* The Graphics layout uses a "magic" formula for how to apply @@ -131,7 +145,7 @@ void MainBox::test3() setRowStretchFactor( 1, 2 ); } -void MainBox::test4() +void Box::test4() { /* When setting a maximum size together with an alignment the expected result @@ -155,7 +169,7 @@ void MainBox::test4() setAlignmentAt( 0, Qt::AlignCenter ); } -void MainBox::test5() +void Box::test5() { /* QGridLayoutEngine does not work correctly when putting more @@ -183,9 +197,12 @@ void MainBox::test5() setFixedSizeAt( 0, QSize( 100, 100 ) ); setFixedSizeAt( 1, QSize( 50, 50 ) ); + + setAlignmentAt( 0, Qt::AlignCenter ); + setAlignmentAt( 1, Qt::AlignCenter ); } -void MainBox::test6() +void Box::test6() { /* QGridLayoutEngine ignores the first column coming from @@ -198,7 +215,7 @@ void MainBox::test6() //setSpacing( 0 ); } -void MainBox::test7() +void Box::test7() { /* This test is very similar to test6, but here we can see a @@ -216,7 +233,7 @@ void MainBox::test7() //setRetainSizeWhenHiddenAt( 0, true ); } -void MainBox::test8() +void Box::test8() { /* This test creates a situation, where we have more space than @@ -243,7 +260,7 @@ void MainBox::test8() } } -void MainBox::test9() +void Box::test9() { /* This one is a bit unclear about how much space from the first @@ -262,6 +279,64 @@ void MainBox::test9() //setColumnFixedWidth( 0, 50 ); } +void Box::test10() +{ + /* + Widgets gives space to the bounded columns 1/2 instead + of 0/3, where they would be more useful. + */ + insert( "PaleVioletRed", 0, 0 ); + insert( "NavajoWhite", 0, 1, 1, 2 ); + insert( "Coral", 0, 3 ); + + setMaximumWidthAt( 1, 70 ); +} + +class MainWidget : public QWidget +{ + public: + MainWidget( int id ) + { + m_box = new Box( this ); + m_box->showTest( id ); + + m_listBox = new QListWidget( this ); + for ( int i = 0; i < m_box->count(); i++ ) + m_listBox->addItem( QStringLiteral( "Test " ) + QString::number( i ) ); + + m_listBox->setCurrentRow( id ); + + connect( m_listBox, &QListWidget::currentRowChanged, + m_box, &Box::showTest ); + } + + protected: + void resizeEvent( QResizeEvent* ) override + { + /* + Not using layouts here to avoid confusion + while debugging. + */ + + const auto r = contentsRect(); + const int spacing = 5; + + auto fm = m_listBox->fontMetrics(); + + const int w1 = fm.width( "Test 100" ) + 20; + const int w2 = r.width() - w1 - spacing; + + m_listBox->setGeometry( r.left(), r.top(), w1, r.height() ); + m_box->setGeometry( r.right() - w2, r.top(), w2, r.height() ); + + m_listBox->setFocus(); + } + + private: + QListWidget* m_listBox; + Box* m_box; +}; + int main( int argc, char** argv ) { QApplication a( argc, argv ); @@ -270,10 +345,10 @@ int main( int argc, char** argv ) if ( argc == 2 ) testcase = atoi( argv[1] ); - MainBox box( testcase ); + MainWidget w( testcase ); - box.resize( 600, 600 ); - box.show(); + w.resize( 600, 600 ); + w.show(); return a.exec(); }