QGraphicsLinearLayout not aligning widgets as expected - c++

I have got a QGraphicsScene that I have added a QGraphicsWidget.
I initially setup the widget this way
AGraphicsWidget::AGraphicsWidget( QGraphicsItem* parent, QGraphicsScene* scene )
: QGraphicsWidget( parent )
, m_mainLayout( new QGraphicsLinearLayout( Qt::Vertical ) )
, m_titleLabel( new QLabel( "Title" ) )
, m_executionPin( new NodeExecutionPin() )
{
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
setGraphicsItem( this );
setFlag( ItemIsMovable, true );
m_mainLayout->setInstantInvalidatePropagation( true );
auto* labelProxy = scene->addWidget( m_titleLabel );
auto* secondLabel = scene->addWidget( new QLabel( "Subtitle" ) );
auto* button = scene->addWidget( new QPushButton( "Another" ) );
m_mainLayout->addItem( labelProxy );
m_mainLayout->addItem( secondLabel );
m_mainLayout->addItem( button );
setLayout( m_mainLayout );
}
This setup does not do what I expect which would be a vertically aligned set of widgets instead I get this
However when I add a text edit as the last element like this
AGraphicsWidget::AGraphicsWidget( QGraphicsItem* parent, QGraphicsScene* scene )
: QGraphicsWidget( parent )
, m_mainLayout( new QGraphicsLinearLayout( Qt::Vertical ) )
, m_titleLabel( new QLabel( "Title" ) )
, m_executionPin( new NodeExecutionPin() )
{
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
setGraphicsItem( this );
setFlag( ItemIsMovable, true );
m_mainLayout->setInstantInvalidatePropagation( true );
auto* labelProxy = scene->addWidget( m_titleLabel );
auto* secondLabel = scene->addWidget( new QLabel( "Subtitle" ) );
auto* button = scene->addWidget( new QPushButton( "Another" ) );
auto* edit = scene->addWidget( new QTextEdit() );
m_mainLayout->addItem( labelProxy );
m_mainLayout->addItem( secondLabel );
m_mainLayout->addItem( button );
m_mainLayout->addItem( edit );
setLayout( m_mainLayout );
}
It works as expected, I get the nice vertical layout, I get the same non alignment as above if I try and nest any QGraphicsLinearLayouts any further where they will overlap each other, what is happening here?
Thanks in advance.

Related

Qwt - Can we plot an Area chart?

Does anyone know if we can plot an area chart using the Qwt library on Qt creator ? If yes, could someone help me with a sample program ?
Based on the screenshots and the listing of examples, I would look at the source for cpuplot.
./qwt-6.1.2/examples>ls
animation curvdemo1 examples.pro radio simpleplot sysinfo
barchart dials friedberg rasterview sinusplot tvplot
bode distrowatch itemeditor realtime spectrogram
controls event_filter legends refreshtest stockchart
cpuplot examples.pri oscilloscope scatterplot stylesheets
./qwt-6.1.2/examples/cpuplot>ls
cpupiemarker.cpp cpuplot.cpp cpuplot.pro cpustat.h
cpupiemarker.h cpuplot.h cpustat.cpp
And
cpuplot.cpp
#include <qapplication.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include <qwt_scale_draw.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_legend_label.h>
#include <qwt_plot_canvas.h>
#include "cpupiemarker.h"
#include "cpuplot.h"
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw( const QTime &base ):
baseTime( base )
{
}
virtual QwtText label( double v ) const
{
QTime upTime = baseTime.addSecs( static_cast<int>( v ) );
return upTime.toString();
}
private:
QTime baseTime;
};
class Background: public QwtPlotItem
{
public:
Background()
{
setZ( 0.0 );
}
virtual int rtti() const
{
return QwtPlotItem::Rtti_PlotUserItem;
}
virtual void draw( QPainter *painter,
const QwtScaleMap &, const QwtScaleMap &yMap,
const QRectF &canvasRect ) const
{
QColor c( Qt::white );
QRectF r = canvasRect;
for ( int i = 100; i > 0; i -= 10 )
{
r.setBottom( yMap.transform( i - 10 ) );
r.setTop( yMap.transform( i ) );
painter->fillRect( r, c );
c = c.dark( 110 );
}
}
};
class CpuCurve: public QwtPlotCurve
{
public:
CpuCurve( const QString &title ):
QwtPlotCurve( title )
{
setRenderHint( QwtPlotItem::RenderAntialiased );
}
void setColor( const QColor &color )
{
QColor c = color;
c.setAlpha( 150 );
setPen( QPen( Qt::NoPen ) );
setBrush( c );
}
};
CpuPlot::CpuPlot( QWidget *parent ):
QwtPlot( parent ),
dataCount( 0 )
{
setAutoReplot( false );
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setBorderRadius( 10 );
setCanvas( canvas );
plotLayout()->setAlignCanvasToScales( true );
QwtLegend *legend = new QwtLegend;
legend->setDefaultItemMode( QwtLegendData::Checkable );
insertLegend( legend, QwtPlot::RightLegend );
setAxisTitle( QwtPlot::xBottom, " System Uptime [h:m:s]" );
setAxisScaleDraw( QwtPlot::xBottom,
new TimeScaleDraw( cpuStat.upTime() ) );
setAxisScale( QwtPlot::xBottom, 0, HISTORY );
setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
/*
In situations, when there is a label at the most right position of the
scale, additional space is needed to display the overlapping part
of the label would be taken by reducing the width of scale and canvas.
To avoid this "jumping canvas" effect, we add a permanent margin.
We don't need to do the same for the left border, because there
is enough space for the overlapping label below the left scale.
*/
QwtScaleWidget *scaleWidget = axisWidget( QwtPlot::xBottom );
const int fmh = QFontMetrics( scaleWidget->font() ).height();
scaleWidget->setMinBorderDist( 0, fmh / 2 );
setAxisTitle( QwtPlot::yLeft, "Cpu Usage [%]" );
setAxisScale( QwtPlot::yLeft, 0, 100 );
Background *bg = new Background();
bg->attach( this );
CpuPieMarker *pie = new CpuPieMarker();
pie->attach( this );
CpuCurve *curve;
curve = new CpuCurve( "System" );
curve->setColor( Qt::red );
curve->attach( this );
data[System].curve = curve;
curve = new CpuCurve( "User" );
curve->setColor( Qt::blue );
curve->setZ( curve->z() - 1 );
curve->attach( this );
data[User].curve = curve;
curve = new CpuCurve( "Total" );
curve->setColor( Qt::black );
curve->setZ( curve->z() - 2 );
curve->attach( this );
data[Total].curve = curve;
curve = new CpuCurve( "Idle" );
curve->setColor( Qt::darkCyan );
curve->setZ( curve->z() - 3 );
curve->attach( this );
data[Idle].curve = curve;
showCurve( data[System].curve, true );
showCurve( data[User].curve, true );
showCurve( data[Total].curve, false );
showCurve( data[Idle].curve, false );
for ( int i = 0; i < HISTORY; i++ )
timeData[HISTORY - 1 - i] = i;
( void )startTimer( 1000 ); // 1 second
connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ),
SLOT( legendChecked( const QVariant &, bool ) ) );
}
void CpuPlot::timerEvent( QTimerEvent * )
{
for ( int i = dataCount; i > 0; i-- )
{
for ( int c = 0; c < NCpuData; c++ )
{
if ( i < HISTORY )
data[c].data[i] = data[c].data[i-1];
}
}
cpuStat.statistic( data[User].data[0], data[System].data[0] );
data[Total].data[0] = data[User].data[0] + data[System].data[0];
data[Idle].data[0] = 100.0 - data[Total].data[0];
if ( dataCount < HISTORY )
dataCount++;
for ( int j = 0; j < HISTORY; j++ )
timeData[j]++;
setAxisScale( QwtPlot::xBottom,
timeData[HISTORY - 1], timeData[0] );
for ( int c = 0; c < NCpuData; c++ )
{
data[c].curve->setRawSamples(
timeData, data[c].data, dataCount );
}
replot();
}
void CpuPlot::legendChecked( const QVariant &itemInfo, bool on )
{
QwtPlotItem *plotItem = infoToItem( itemInfo );
if ( plotItem )
showCurve( plotItem, on );
}
void CpuPlot::showCurve( QwtPlotItem *item, bool on )
{
item->setVisible( on );
QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );
QList<QWidget *> legendWidgets =
lgd->legendWidgets( itemToInfo( item ) );
if ( legendWidgets.size() == 1 )
{
QwtLegendLabel *legendLabel =
qobject_cast<QwtLegendLabel *>( legendWidgets[0] );
if ( legendLabel )
legendLabel->setChecked( on );
}
replot();
}
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QWidget vBox;
vBox.setWindowTitle( "Cpu Plot" );
CpuPlot *plot = new CpuPlot( &vBox );
plot->setTitle( "History" );
const int margin = 5;
plot->setContentsMargins( margin, margin, margin, margin );
QString info( "Press the legend to en/disable a curve" );
QLabel *label = new QLabel( info, &vBox );
QVBoxLayout *layout = new QVBoxLayout( &vBox );
layout->addWidget( plot );
layout->addWidget( label );
vBox.resize( 600, 400 );
vBox.show();
return a.exec();
}
Hope that helps.

QScrollArea with dynamically resizing subWidgets

So I have something like the following layout in my Qt Application.
QScroll Area
- QSrollArea's InternalWidget
-QVBoxLayout
-Layout 1
- some items
- QTableView
-Layout 2
- some items
- QTableView
The contents of the QTableViews is changing dynamically, and what i want is that each table view to be as large as it has to be( without progressbars and without empty space ). I have written a function to calculate the appropriate size of a table. The problem is that when i dynamically resize one of the TableViews it goes behind the second view( and what should happen is that the whole second layout be moved bellow the first ). Furthermore when shrink the table view there is an empty space left between it and the second layout.
Here is the code when i arrange the widgets:
#include "Widget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QTableView>
#include <QStringBuilder>
#include <QHeaderView>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent),
m_tableView1( 0 ),
m_tableView2( 0 ),
m_model1( 0 ),
m_model2( 0 ),
m_numberOfRowsEdit( 0 )
{
this->resize( 300, 520 );
QVBoxLayout* mainLayout = new QVBoxLayout( this );
QScrollArea* mainArea = new QScrollArea();
//mainArea->setWidgetResizable( true );
QWidget* scrollAreaWidget = new QWidget;
scrollAreaWidget->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
QVBoxLayout* scrollAreaWidgetLayout = new QVBoxLayout( scrollAreaWidget );
scrollAreaWidgetLayout->setSizeConstraint( QLayout::SetMinAndMaxSize );
QVBoxLayout* firstSubLayout = new QVBoxLayout;
QLabel* label = new QLabel( "Label 1" );
m_tableView1 = new QTableView;
firstSubLayout->addWidget( label );
firstSubLayout->addWidget( m_tableView1 );
scrollAreaWidgetLayout->addLayout( firstSubLayout );
QVBoxLayout* secondSubLayout = new QVBoxLayout;
QLabel* label2 = new QLabel( "Label 2" );
m_tableView2 = new QTableView;
secondSubLayout->addWidget( label2 );
secondSubLayout->addWidget( m_tableView2 );
scrollAreaWidgetLayout->addLayout( secondSubLayout );
mainArea->setWidget( scrollAreaWidget );
mainLayout->addWidget( mainArea );
// Utility for dynamically changing rows
QHBoxLayout* hLayout = new QHBoxLayout;
QLabel* numberOfRowsLabel = new QLabel( "Number of rows" );
m_numberOfRowsEdit = new QLineEdit;
QPushButton* numberOfRowsButton = new QPushButton( "Apply" );
connect( numberOfRowsButton, SIGNAL(clicked()), SLOT(onApplyButtonPressed()) );
hLayout->addWidget( numberOfRowsLabel );
hLayout->addWidget( m_numberOfRowsEdit );
hLayout->addWidget( numberOfRowsButton );
m_model1 = new QStandardItemModel( this );
m_tableView1->setModel( m_model1 );
m_model2 = new QStandardItemModel( this );
m_tableView2->setModel( m_model2 );
mainLayout->addLayout( hLayout );
}
Widget::~Widget()
{
}
QSize Widget::calculateTableDesiredSize( QTableView* const table ) {...}
void Widget::onApplyButtonPressed()
{
bool ok = false;
const int rowCount = m_numberOfRowsEdit->text().toInt( &ok );
if ( !ok )
{
return;
}
this->initModel( m_model1, rowCount );
}
// inits model with rowCount rows
void Widget::initModel( QStandardItemModel* const model, const int rowCount )
void Widget::resizeTable( QTableView* const table )
You should use setFixedHeight() instead of resize() to set table height. Also you should addStretch() to scrollAreaWidgetLayout after all its items.

Qt exit when hiding window

I have a MainWindow which calls the LoginWindow in the constructor. The LoginDialog has a button to create an account which will create a QDialog.
I wanted to hide the LoginDialog while the Dialog for the new account is showing but somehow it crashes.
When I remove the first and last line of the function which hides and shows the LoginDialog it's absolutely fine. Why does it crash with hide() and show() are called ?
void LoginDialog::createAccount()
{
// (-> will cause crash later) hide(); //Hides LoginDialog
QDialog dlg;
dlg.setGeometry( this->x(), this->y(), this->width(), this->height() );
QWidget* centralWidget = new QWidget( &dlg );
QVBoxLayout* l = new QVBoxLayout( centralWidget );
dlg.setLayout( l );
QLineEdit *dlgUser = new QLineEdit( centralWidget );
QLineEdit *dlgPass = new QLineEdit( centralWidget );
dlgPass->setEchoMode( QLineEdit::Password );
l->addWidget( new QLabel( tr("Username :"), centralWidget ) );
l->addWidget( dlgUser );
l->addWidget( new QLabel( tr("Password :"), centralWidget ) );
l->addWidget( dlgPass );
l->addWidget( new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, centralWidget ) );
if( dlg.exec() != QDialog::Rejected )
{
;
}
delete centralWidget;
// (-> will cause crash later) show(); //Show LoginDialog again
}
There are no errors, it just crashes unexpectedly and sometimes it exits with code (0).
When analyzing with the debugger and really going through every single step it doesn't crash. The LoginDialog will be shown and it's not going to crash.
I don't get the purpose of your centralWidget in the dialog? I don't think it is needed at all, and you can assemble your widgets directly in the dialog. I would rewrite your code in this way:
void LoginDialog::createAccount()
{
QDialog dlg;
dlg.setGeometry( this->x(), this->y(), this->width(), this->height() );
QLineEdit *dlgUser = new QLineEdit( &dlg );
QLineEdit *dlgPass = new QLineEdit( &dlg );
dlgPass->setEchoMode( QLineEdit::Password );
QVBoxLayout* l = new QVBoxLayout;
l->addWidget( new QLabel( tr("Username :"), &dlg ) );
l->addWidget( dlgUser );
l->addWidget( new QLabel( tr("Password :"), &dlg ) );
l->addWidget( dlgPass );
l->addWidget( new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dlg ) );
dlg.setLayout( l );
if( dlg.exec() != QDialog::Rejected )
{
// Do something.
}
}

Resize a wxNotebook when content sizes changed

When dynamically changing the size of the elements within a wxNotebook (e.g. hiding a button), the size of the whole notebook element is not updated. This can leave white space or prevent some elements from being shown.
Normally, a call to the parent window's Fit() or FitInside() should do the trick, but doesn't for the notebook. How can we resize it?
Minimal example demonstrating the problem:
#include <wx/wx.h>
#include <wx/gbsizer.h>
#include <wx/notebook.h>
const int HIDEME_ID = 512, SHOWME_ID = 513;
class MyApp: public wxApp
{
wxButton* hideme;
wxSizer* panel_sizer, *frame_sizer, *sw_sizer;
wxScrolledWindow* sw;
wxNotebook* nb;
void hide_button( wxCommandEvent& ) {
hideme->Hide();
relayout();
}
void relayout() {
/* Insert layout code here */
}
void show_button( wxCommandEvent& ) {
hideme->Show();
relayout();
}
virtual bool OnInit() {
wxFrame *frame = new wxFrame( NULL, wxID_ANY, _("Hello World"), wxPoint(50, 50), wxSize(450, 340) );
frame_sizer = new wxBoxSizer( wxVERTICAL );
frame->SetSizer( frame_sizer );
sw = new wxScrolledWindow( frame, wxID_ANY );
sw->SetScrollRate( 5, 5 );
frame_sizer->Add( sw, 1, wxEXPAND );
sw_sizer = new wxBoxSizer( wxVERTICAL );
sw->SetSizer( sw_sizer );
nb = new wxNotebook( sw, wxID_ANY );
wxPanel* panel = new wxPanel( nb, wxID_ANY );
nb->AddPage( panel, wxT("Tab1") );
sw_sizer->Add( new wxButton( sw, wxID_ANY, _("Button1"), wxDefaultPosition, wxSize(200,200) ), 0, wxEXPAND );
sw_sizer->Add( nb, 0, wxEXPAND );
sw_sizer->Add( new wxButton( sw, wxID_ANY, _("Button3") ), 1, wxEXPAND );
sw_sizer->Add( new wxButton( sw, wxID_ANY, _("Button4") ), 0, wxEXPAND );
panel_sizer = new wxBoxSizer( wxVERTICAL );
panel_sizer->Add( hideme = new wxButton(panel, HIDEME_ID, wxT("HideMe")), 0, wxEXPAND );
hideme->Hide();
panel_sizer->Add( new wxButton(panel, SHOWME_ID, wxT("ShowMe")), 0, wxEXPAND );
panel->SetSizer( panel_sizer );
frame->Show(true);
SetTopWindow(frame);
return true;
}
DECLARE_EVENT_TABLE();
};
BEGIN_EVENT_TABLE(MyApp, wxApp)
EVT_BUTTON(HIDEME_ID, MyApp::hide_button)
EVT_BUTTON(SHOWME_ID, MyApp::show_button)
END_EVENT_TABLE()
IMPLEMENT_APP( MyApp );
The somewhat surprising pitfall here is that, unlike many other windows, a wxNotebook caches its best size. We have to invalidate the cache before doing any layouting:
nb->InvalidateBestSize();
sw->FitInside();

How to set Qt QStateMachine animation duration

I'm trying to learn the Qt framework. My QStateMachine code does the correct thing (pressing the button makes the chat window popup change size).
I can't change the speed of the animation to get a nice visual transition.
Any suggestions?
Here's the code:
MainWindow::MainWindow()
{
widget.setupUi( this );
// chat window - Chat button opens
ChatWindowClosedState = new QState();
ChatWindowOpenState = new QState();
ChatWindowOpenGeometry = widget.groupBox->geometry();
ChatWindowClosedGeometry = widget.pushButton->geometry();
ChatWindowClosedGeometry.translate( -ChatWindowClosedGeometry.width(), 0 );
ChatWindowClosedState->assignProperty( widget.groupBox, "geometry", ChatWindowClosedGeometry );
ChatWindowOpenState->assignProperty( widget.groupBox, "geometry", ChatWindowOpenGeometry );
ChatWindowCloseTransition = ChatWindowClosedState->addTransition( widget.pushButton, SIGNAL( clicked() ), ChatWindowOpenState );
ChatWindowCloseAnimation = new QPropertyAnimation( widget.pushButton, "geometry" );
ChatWindowCloseAnimation->setDuration( 5000 );
ChatWindowCloseTransition->addAnimation( ChatWindowCloseAnimation );
ChatWindowOpenTransition = ChatWindowOpenState->addTransition( widget.pushButton, SIGNAL( clicked() ), ChatWindowClosedState );
ChatWindowOpenAnimation = new QPropertyAnimation( widget.pushButton, "geometry" );
ChatWindowOpenAnimation->setDuration( 5000 );
ChatWindowOpenTransition->addAnimation( ChatWindowOpenAnimation );
machine = new QStateMachine( this );
machine->addState( ChatWindowClosedState );
machine->addState( ChatWindowOpenState );
machine->setInitialState( ChatWindowClosedState );
machine->start();
}
The code
ChatWindowOpenAnimation = new QPropertyAnimation( widget.pushButton, "geometry" );
should be
ChatWindowOpenAnimation = new QPropertyAnimation( widget.groupBox, "geometry" );
The animation was being applied to the wrong widget.