I have a QComboBox with a QStandardItemModel, which contains a single item named One.
I want the QComboBox to have an header (I’m not sure this is the correct technical term …) which will always be the same.
The following image depicts exactly what I want. Instead of having "One" next to the button that push down the list, I want "Header" printed (which is not an element of the list).
Important, the checkbox are mandatory (which is why I'm using QComboBox).
I tried the function model.setHorizontalHeaderItem() but it does not work (see the code below).
Please, help me.
#include <QApplication>
#include <QComboBox>
#include <QStandardItemModel>
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QComboBox* comboBox = new QComboBox();
QStandardItemModel model( 1, 1 );
QStandardItem *item = new QStandardItem( QString("One") );
item->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );
item->setData ( Qt::Unchecked, Qt::CheckStateRole );
model.setItem(0, 0, item);
model.setHorizontalHeaderItem( 0, new QStandardItem( "Header" ) );
comboBox->setModel( &model );
comboBox->show();
return app.exec();
}
You can do something like this:
QTreeView *view = new QTreeView();
QStandardItemModel *model = new QStandardItemModel();
ui->comboBox->setModel( model );
ui->comboBox->setView( view );
for ( int i = 0; i < 10; i++ )
{
QStandardItem *item = new QStandardItem();
const QString text = QString( "Item: %1" ).arg( i + 1 );
item->setText( text );
model->appendRow( item );
}
model->setHorizontalHeaderLabels( QStringList() << "It's a column" );
You will get something like this:
Now you can do all customization like with a standard QTreeView.
Related
I'm running the sample code below with Qt5.11.0 on OSX 10.13.6 (also RHEL 7.6, where the problem also occurs, but not as ugly as on OSX). The test program displays a custom model in a QTableView, with indexWidgets set for several of the columns:
#include <QtCore/QDebug>
#include <QtCore/QAbstractItemModel>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QTableView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
class AModel : public QAbstractItemModel
{
public:
int rowCount( const QModelIndex& parent = QModelIndex() ) const override {
return 5;
};
int columnCount( const QModelIndex& parent = QModelIndex() ) const override {
return 5;
};
QModelIndex parent( const QModelIndex& index ) const override {
return QModelIndex();
};
QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const override {
if( ( ! parent.isValid() ) &&
row >= 0 && row < 5 &&
column >= 0 && column < 5 ) {
return createIndex( row, column );
} else {
return QModelIndex();
}
};
QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override {
QVariant qval;
if( index.column() >= 1 && index.column() < 4 ) { return QVariant(); }
switch( role ) {
case Qt::DisplayRole:
qval = QString( "%1,%2" ).arg( index.row() ).arg( index.column() );
break;
default:
qval = QVariant();
break;
}
return qval;
};
};
class AWidget : public QWidget
{
public:
AWidget( QWidget* parent ) : QWidget( parent ) {
QHBoxLayout* l = new QHBoxLayout();
this->setLayout( l );
QRadioButton* save = new QRadioButton( "Save" );
QRadioButton* del = new QRadioButton( "Delete" );
l->addWidget( save );
l->addWidget( del );
};
};
int
main( int argc, char *argv[] ) {
QApplication app( argc, argv );
QMainWindow* mw = new QMainWindow();
AModel* model = new AModel();
QTableView* view = new QTableView();
view->setModel( model );
// view->verticalHeader()->setDefaultSectionSize( 15 );
for( int irow = 0; irow < model->rowCount(); irow++ ) {
QPushButton* pb = new QPushButton( "Mogrify", mw );
QRadioButton* rb = new QRadioButton( "Choose", mw );
AWidget* aw = new AWidget( mw );
QObject::connect( pb, &QPushButton::clicked, [irow](){ qDebug() << "Mogrifying " << irow; } );
QObject::connect( rb, &QRadioButton::clicked, [irow](){ qDebug() << "Choosing " << irow; } );
view->setIndexWidget( model->index( irow, 1 ), pb );
view->setIndexWidget( model->index( irow, 2 ), rb );
view->setIndexWidget( model->index( irow, 3 ), aw );
}
view->resizeColumnsToContents();
mw->setCentralWidget( view );
mw->show();
return app.exec();
}
If I just run this as shown above, the result comes out with all the table-embedded widgets having enough space:
If however I uncomment the call to setDefaultSectionSize() in the code above, the table-embedded widgets do not size themselves the way I'd wish. The QPushButton is cut off at the bottom, the QRadioButton is crammed in with little padding, and the custom composite widget doesn't show up at all:
I've tried all manner of QSizeHint experiments, subclassing, and internet searching to get these embedded widgets to size themselves according to the space available in the table cells, so far to no avail. How do I make these embedded indexWidgets paint themselves so they fit into the cell space provided in the QTableView, when I'm telling the QTableView how big its cells should be?
The problem is not in the QTableView but in your custom widget. The custom widget has a layout that must have margins equal to 0.
class AWidget : public QWidget
{
public:
AWidget( QWidget* parent=nullptr) :
QWidget( parent )
{
QHBoxLayout* l = new QHBoxLayout(this);
l->setContentsMargins(0, 0, 0, 0); // <----
QRadioButton* save = new QRadioButton( "Save" );
QRadioButton* del = new QRadioButton( "Delete" );
l->addWidget( save );
l->addWidget( del );
};
};
I have a custom storage and I want to implement ListModel to display it with QComboBox. For simplicity let's assume that we have a list of int as a model's data, so here is my implementation of a model and test program:
#include <QApplication>
#include <QDebug>
#include <QAbstractListModel>
#include <QComboBox>
#include <QHBoxLayout>
#include <QPushButton>
QList<int> list;
class MyModel : public QAbstractListModel
{
public:
MyModel( QWidget* parent ):
QAbstractListModel( parent )
{}
~MyModel()
{
qDebug() << __FUNCTION__;
}
QVariant data(const QModelIndex &index, int role) const
{
if ( !index.isValid() )
return QVariant();
if ( ( role == Qt::DisplayRole ) && ( index.row() < list.size() ) )
return QString::number( list.at( index.row() ) );
return QVariant();
}
int rowCount(const QModelIndex &parent) const
{
Q_UNUSED( parent )
return list.size();
}
};
int main ( int argc, char* argv[] )
{
QApplication app ( argc, argv );
QWidget w;
QHBoxLayout* l = new QHBoxLayout();
QComboBox* c = new QComboBox( &w );
c->setModel( new MyModel( c ) );
l->addWidget( c );
QPushButton* b = new QPushButton("+");
QObject::connect( b, &QPushButton::clicked, [](){
list.push_back( qrand() );
qDebug() << list;
} );
l->addWidget( b );
b = new QPushButton("-");
QObject::connect( b, &QPushButton::clicked, [](){
if ( !list.isEmpty() )
list.pop_back();
qDebug() << list;
} );
l->addWidget( b );
w.setLayout( l );
w.show();
return app.exec ();
}
If I hit button Add only once and then check list, it looks okay, but when I hit it again, QComboBox displays only the first value and an empty line; continuing adding new elements has no effect at QComboBox.
If I click on button Add many times, then I can see the correct list in ComboBox.
I can't understand what is going on and what I do wrong.
I am using Qt5.5.1 with VS2013 x32 on Windows 7.
Your model needs to emit proper signals when the data actually changes. This is generally done by calling protected functions beginInsertRows, endInsertRows, etc... As these are protected, they can be called only by member functions inside MyModel. Also, for good practice, the actual data (your list) should be modified only by MyModel.
First, make list a private member of MyModel. It should not be accessed from the outside.
private:
QList<int> list;
Add two public functions inside MyModel for managing list:
public:
void push(int value)
{
beginInsertRows(list.size(), list.size());
list.push_back(value);
endInsertRows();
}
void pop()
{
if(list.size()>0)
{
beginRemoveRows(list.size()-1, list.size()-1);
list.pop_back();
endRemoveRows();
}
}
Inside main, keep a pointer on your model
MyModel * m = new MyModel(c);
Then use these functions inside main() instead of appending to list directly.
QObject::connect( b, &QPushButton::clicked, [m](){
m->push(qrand());
} );
QObject::connect( b, &QPushButton::clicked, [m](){
m->pop();
} );
Et voila
Alternative, more Qt way
Declare push and pop as slots:
public slots:
void push()
{
beginInsertRows(list.size(), list.size());
list.push_back(qrand());
endInsertRows();
}
void pop()
{
if(list.size()>0)
{
beginRemoveRows(list.size()-1, list.size()-1);
list.pop_back();
endRemoveRows();
}
}
And connect them directly to push buttons in main:
QObject::connect( b, &QPushButton::clicked, m, &MyModel::push);
//...
QObject::connect( b, &QPushButton::clicked, m, &MyModel::pop);
The problem with this code is that model doesn't know that data inside list is changed. And the QComboBox doesn't know about data inside model changed too. So each time you change the data inside list your model should emit signals layoutAboutToBeChanged() and then layoutChanged() to notify QComboBox about changes.
You have to implement additional functions, at least AbstractItemModel::insertRows. You have to notify a model about changes. For more information look into docs.
I seem to be having an issue with getting the behavior I want from a QScrollArea. As it stands, whenever I add something to the layout of the widget set as the target of the scroll area it will rather opt to expand the entire window rather than fit to scroll.
Here's my current setup:
QSplitter * mainArea = new QSplitter( Qt::Vertical );
QWidget * containment = new QWidget;
containment->setLayout( new QVBoxLayout );
currentStructures = new QWidget;
currentStructures->setLayout( new QVBoxLayout );
currentStructures->layout()->setAlignment( Qt::AlignTop );
QScrollArea * scroll = new QScrollArea();
scroll->setWidget( currentStructures );
containment->layout()->addWidget( currentStructures );
mainArea->addWidget( containment );
mainArea->addWidget( new QWidget ); //TODO: create preview bar
this->layout()->addWidget( mainArea );
This makes it so that the scroll area only ever expands and never shows the scroll bars.
By inserting this line:
containment->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Ignored );
I can get the area to ignore the size of its children but it also doesn't take up the space needed nor does it show scroll bars -- it just crunches the widget inside of it.
I'm a bit of a greenhorn to using Qt, but I was wondering how I would achieve the behavior that I need: I would like the scroll area to greedily take up the area it has available from its parent layout but not expand the the containing layouts vertically when adding but instead actually shows scroll bars. I'm planning on allowing a lot of resizing, so it needs to actually scale to the parent instead of just being a fixed size. I am at a loss as to how I should proceed in solving this aspect of my GUI. Thank you for your time.
If you would like to tackle this problem visually, here's a harness that you can use. I greatly appreciate your help.
#include <QtWidgets\qapplication.h>
#include <QtWidgets\qsplitter.h>
#include <QtWidgets\qlayout.h>
#include <QtWidgets\qscrollarea.h>
#include <QtWidgets\qpushbutton.h>
#include <QtWidgets\qlabel.h>
#include <QtWidgets\qsizepolicy.h>
int main( int argc, char * argv[] )
{
QApplication app( argc, argv );
QWidget * testWidget = new QWidget;
testWidget->setLayout( new QVBoxLayout );
//////////////////CODE IN QUESTIION//////////////////////
QSplitter * mainArea = new QSplitter( Qt::Vertical );
QWidget * containment = new QWidget;
containment->setLayout( new QVBoxLayout );
//containment->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Ignored );
QWidget * currentStructures = new QWidget;
currentStructures->setLayout( new QVBoxLayout );
currentStructures->layout()->setAlignment( Qt::AlignTop );
QScrollArea * scroll = new QScrollArea();
scroll->setWidget( currentStructures );
containment->layout()->addWidget( currentStructures );
mainArea->addWidget( containment );
///////////////////////////////////////////////////////////
QPushButton * pushIntoLayout = new QPushButton( "Add Element to Widget" );
QWidget::connect( pushIntoLayout, &QPushButton::clicked, [currentStructures](){ currentStructures->layout()->addWidget( new QLabel( "A generated label" ) ); } );
mainArea->addWidget( pushIntoLayout );
currentStructures->setStyleSheet(
"QWidget {"
"background-color: #FAA;"
"}"
);
testWidget->layout()->addWidget( mainArea );
testWidget->show();
return app.exec();
}
Even after copying and pasting my code to make a test harness I had not noticed my grievous error. I had mistakenly pushed the currentStructures widget to the container's layout rather than scroll, the scrolling area after giving it its child.
An excerpt from the Qt docs on void QScrollArea::setWidget( QWidget * widget ) for those unfamiliar:
The widget becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.
Thanks to everyone who looked it over.
For those wondering, the fixed code would look like the following:
QSplitter * mainArea = new QSplitter( Qt::Vertical );
QWidget * containment = new QWidget;
containment->setLayout( new QVBoxLayout );
QWidget * currentStructures = new QWidget;
currentStructures->setLayout( new QVBoxLayout );
currentStructures->layout()->setAlignment( Qt::AlignTop );
QScrollArea * scroll = new QScrollArea();
scroll->setWidget( currentStructures );
scroll->setWidgetResizable( true );
containment->layout()->addWidget( scroll );
mainArea->addWidget( containment );
mainArea->addWidget( new QWidget ); //TODO: create preview bar
this->layout()->addWidget( mainArea );
Cheers!
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.
It seems like this would be a common thing to do, but I can't find how.
I have a QTextEdit or QPlainTextEdit widget with a bunch of text. Enough that scrolling is necessary.
I want another widget to give some information about the currently visible text. To do this, I need to know
when the visible text changes
what's the text?
I see that QPlainTextEdit has the method firstVisibleBlock, but it's protected. This tells me that it's not really something I should be using in my application. I wouldn't otherwise need to subclass from the edit window.
I also see that there's the signal updateRequest but it's not clear what I do with the QRect.
How do I do it or where do I find a hint?
I've written a minimal program that as two QTextEdit fields. In the left field you write and the text you are writing is shown in the second text edit too. You get the text of a QTextEdit by using toPlainText() and the signal is textChanged().
I've tested it and what you write in m_pEdit_0 is shown in "real-time" in m_pEdit_1.
main_window.hpp
#ifndef __MAIN_WINDOW_H__
#define __MAIN_WINDOW_H__
#include <QtGui/QtGui>
#include <QtGui/QMainWindow>
#include <QtGui/QApplication>
class main_window : public QMainWindow
{
Q_OBJECT
public:
main_window( QWidget* pParent = 0 );
~main_window();
public Q_SLOTS:
void on_edit_0_text_changed();
private:
QHBoxLayout* m_pLayout;
QTextEdit* m_pEdit_0;
QTextEdit* m_pEdit_1;
};
#endif // !__MAIN_WINDOW_H__
main_window.cpp
#include "main_window.hpp"
main_window::main_window( QWidget *pParent ) : QMainWindow( pParent )
{
m_pEdit_0 = new QTextEdit( this );
m_pEdit_1 = new QTextEdit( this );
connect( m_pEdit_0, SIGNAL( textChanged() ), this, SLOT( on_edit_0_text_changed() ) );
m_pLayout = new QHBoxLayout;
m_pLayout->addWidget( m_pEdit_0 );
m_pLayout->addWidget( m_pEdit_1 );
QWidget* central_widget = new QWidget( this );
central_widget->setLayout( m_pLayout );
setCentralWidget( central_widget );
}
main_window::~main_window()
{
}
void main_window::on_edit_0_text_changed()
{
m_pEdit_1->setText( m_pEdit_0->toPlainText() );
}
main.cpp
#include "main_window.hpp"
int main( int argc, char* argv[] )
{
QApplication a(argc, argv);
main_window mw;
mw.show();
return a.exec();
}
Edit:
This would work too, but would lack in performance for huge documents:
void main_window::on_edit_0_text_changed()
{
QStringList text_in_lines = m_pEdit_0->toPlainText().split( "\n" );
m_pEdit_1->clear();
for( int i = 0; i < text_in_lines.count(); i++ )
{
m_pEdit_1->append( text_in_lines.at( i ) );
}
}