QTableWidget: Prioritize horizontal space for a specific column - c++

I have a QTableWidget that has a column (#3) that needs more space than others. I want to resize all columns to their contents, and give priority to column #3. If column #3 pushes the table's width past what's available, I want column #3 to be truncated with '...' without a horizontal scrollBar.
The screenshot below is the simplest example of the behavior I'm chasing, but I've had to manually adjust the column widths. I want the table to do this automatically.
The following code shows examples of what I've tried on QTableWidget, but none have worked. I've provided inline comments on why the following methods do not work:
table->horizontalHeader()->setStretchLastSection(true);
table->resizeColumnsToContents();
Thank you to anyone who volunteers your time to help me with this.
#include <QApplication>
#include <QTableWidget>
#include <QStringList>
#include <QRect>
#include <QLayout>
#include <QDialog>
#include <QHeaderView>
#include <iostream>
int main( int argc, char* argv[] )
{
QApplication a(argc, argv);
QDialog* d = new QDialog();
d->setLayout( new QVBoxLayout() );
QTableWidget* table = new QTableWidget(1,4);
QStringList headers = {"1", "2", "3", "4"};
table->setHorizontalHeaderLabels(headers);
table->setItem(0, 0, new QTableWidgetItem("1"));
table->setItem(0, 1, new QTableWidgetItem("22222"));
table->setItem(0, 2, new QTableWidgetItem("33333333333333333333333333333"));
table->setItem(0, 3, new QTableWidgetItem("4"));
// Do nothing
//
// The table exceeds the dimensions of the dialog,
// and we get a horizontal scrollbar
// This also results in a horizontal scrollbar
//
// table->horizontalHeader()->setStretchLastSection(true);
// Resizing the columns introduces a horizontal scrollbar, and
// prevents the user from from changing column width
//
// table->resizeColumnsToContents();
// The table fits, but all columns are equally spaced.
// (We want column 3 to take up as much space as possible)
//
// table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// Columns are resized to their contents,
// but column 3 is not truncated and we get a horizontal scrollBar
//
// table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
d->layout()->addWidget( table );
d->show();
return a.exec();
}

This should work, try it out
#include <QApplication>
#include <QTableWidget>
#include <QStringList>
#include <QRect>
#include <QLayout>
#include <QDialog>
#include <QHeaderView>
#include <QScrollBar>
#include <iostream>
int main( int argc, char* argv[] )
{
QApplication a(argc, argv);
QDialog* d = new QDialog();
d->setLayout( new QVBoxLayout() );
QTableWidget* table = new QTableWidget(1,4);
QStringList headers = {"1", "2", "3", "4"};
table->setHorizontalHeaderLabels(headers);
table->setItem(0, 0, new QTableWidgetItem("1"));
table->setItem(0, 1, new QTableWidgetItem("22222"));
table->setItem(0, 2, new QTableWidgetItem("33333333333333333333333333333"));
table->setItem(0, 3, new QTableWidgetItem("4"));
// Do nothing
//
// The table exceeds the dimensions of the dialog,
// and we get a horizontal scrollbar
// This also results in a horizontal scrollbar
//
// table->horizontalHeader()->setStretchLastSection(true);
// Resizing the columns introduces a horizontal scrollbar, and
// prevents the user from changing column width
//
//resize table
d->layout()->addWidget( table );
d->show();
table->resizeColumnsToContents();
int tableWidth = table->width();
int columsWidth = 0;
int maxColumnWidth = 0;
int maxColumnIndex = 0;
int w = 0;
for(int n = 0; n < table->columnCount(); n++)
{
w = table->columnWidth(n);
columsWidth += w;
if(w > maxColumnWidth)
{
maxColumnWidth = w;
maxColumnIndex = n;
}
}
if(columsWidth > tableWidth)
{
int delta = columsWidth - tableWidth + table->horizontalScrollBar()->height();
maxColumnWidth -= delta;
if(maxColumnWidth < 0)
maxColumnWidth = 0;
table->setColumnWidth(maxColumnIndex, maxColumnWidth);
}
// This table fits, but all the columns are equally spaced.
// (We want column 3 to take up as much space as possible)
//
// table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// Columns are resized to their contents,
// but column 3 is not truncated and we get a horizontal scrollBar
//
// table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
return a.exec();
}
Get back to me if you have any problems with this code.

Related

Changing QGraphicsItem stack order with stackBefore

I have a QGraphicsItem "p" with 4 children a, b, c and d, inserted in that order.
#include <QtWidgets/QApplication>
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QGraphicsItem>
#include <QtWidgets/QGraphicsView>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsScene scene(0, 0, 200, 200);
QGraphicsView view(&scene);
QGraphicsItem* p = new QGraphicsRectItem(nullptr);
scene.addItem(p);
QGraphicsRectItem* a = new QGraphicsRectItem( 0, 0, 40, 40, p);
QGraphicsRectItem* b = new QGraphicsRectItem(10, 10, 40, 40, p);
QGraphicsRectItem* c = new QGraphicsRectItem(20, 20, 40, 40, p);
QGraphicsRectItem* d = new QGraphicsRectItem(30, 30, 40, 40, p);
// cosmetic
p->moveBy(40, 40);
a->setBrush(Qt::blue);
b->setBrush(Qt::red);
c->setBrush(Qt::yellow);
d->setBrush(Qt::green);
view.show();
return app.exec();
}
I want to put item a between c and d like this:
So basically I use stackBefore and do:
a->stackBefore(d);
But it doesn't work. So I took a look at QGraphicsItem's code and it seems like if the item is already stacked before (immediately or not) it will not move:
// Only move items with the same Z value, and that need moving.
int siblingIndex = sibling->d_ptr->siblingIndex;
int myIndex = d_ptr->siblingIndex;
if (myIndex >= siblingIndex) {
I can either do:
b->stackBefore(a);
c->stackBefore(a);
which move all elements under a.
or:
a->setParentItem(nullptr);
a->setParentItem(p);
a->stackBefore(d);
which remove a, and re-insert it on top so I can use stackBefore.
Both solutions look no very efficient. The first one, does not scale and the second one lack semantics.
Is there an elegant way to achieve this ?
I would assign an explicit Z value for each graphics item:
int z = 0;
a->setBrush(Qt::blue);
a->setZValue(++z);
b->setBrush(Qt::red);
b->setZValue(++z);
c->setBrush(Qt::yellow);
c->setZValue(++z);
d->setBrush(Qt::green);
d->setZValue(++z);
And then, before using stackBefore(), change the Z value of the moving item:
a->setZValue(d->zValue());
a->stackBefore(d);

Problem Changing Regular Value Axis to Log Axis

I have a QT chart with regular value axes. When I toggle a checkbox, I want to change the X Axis from a QValueAxis to a QLogValueAxis. The problem is, when I do this, my data no longer plots to the correct point.
I've tried two approaches (and a bunch of variations on them) to get the log scale to work, but no what I've tried it seems like the data scales itself to fit in the window linearly and ignores the log axis completely.
Approach 1 - Replace the old axis:
QLogValueAxis* xLogAxis = new QLogValueAxis();
xLogAxis->setBase(10);
xLogAxis->setMinorTickCount(10);
dataSeries->attachedAxes()[0] = xLogAxis; //Replace the old X Axis on the series
chart->setAxisX(xLogAxis);
Approach 2 - Make a completely new Chart:
chart->removeSeries(data); //release the data so you don't destroy it
QChart* newChart = new QChart();
ui->graphView->setChart(newChart); //Swap the old chart out then delete it
delete chart;
chart = newChart;
//get rid of the old axes on the data
for(QAbstractAxis* axis : data->attachedAxes()){
data->detachAxis(axis);
}
QValueAxis* yAxis = new QValueAxis();
data->attachAxis(xLogAxis);
data->attachAxis(yAxis);
chart->addAxis(xLogAxis, Qt::AlignBottom);
chart->addAxis(yAxis, Qt::AlignLeft);
chart->addSeries(data);
chart->legend()->setVisible(false);
Any ideas on how to hotswap to a log axis at runtime? Thanks in advance!
In the following example how to exchange axis types:
#include <QtWidgets>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
typedef std::function<qreal (const qreal &)> function;
static std::vector<std::pair<function, std::string>> functions{
{[](const qreal & v){ return v;}, "linear"},
{[](const qreal & v){ return v*v; }, "quadratic"},
{[](const qreal & v){ return std::exp(0.01*v);}, "exponential"},
{[](const qreal & v){ return std::sqrt(1 + std::abs(v));}, "square root"}
};
class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent),
xLogAxis(new QLogValueAxis),
xLinAxis(new QValueAxis),
yAxis(new QValueAxis)
{
xLogAxis->setBase(10);
xLogAxis->setMinorTickCount(10);
view.setChart(&chart);
checkbox.setText("Log Axis");
connect(&checkbox, &QCheckBox::stateChanged, this, &Widget::onStateChanged);
QVBoxLayout *lay = new QVBoxLayout(this);
lay->addWidget(&checkbox);
lay->addWidget(&view);
chart.addAxis(yAxis, Qt::AlignLeft);
// chart.legend()->hide();
// create series
for(const std::pair<function, std::string> & func: functions){
QLineSeries *serie = new QLineSeries;
serie->setName(QString::fromStdString(func.second));
for(int i=0; i< 1000; ++i){
*serie << QPointF(i+1, func.first(i));
}
chart.addSeries(serie);
serie->attachAxis(yAxis);
}
onStateChanged(checkbox.checkState());
}
private slots:
void onStateChanged(int state){
QAbstractAxis *removeaxis, *insertaxis;
if(state == Qt::Checked){
removeaxis = xLinAxis;
insertaxis = xLogAxis;;
}
else{
removeaxis = xLogAxis;
insertaxis = xLinAxis;
}
if(chart.axes(Qt::Horizontal).contains(removeaxis))
chart.removeAxis(removeaxis);
chart.addAxis(insertaxis, Qt::AlignBottom);
for(auto serie: chart.series()){
if(serie->attachedAxes().contains(removeaxis))
serie->detachAxis(removeaxis);
serie->attachAxis(insertaxis);
}
}
private:
QCheckBox checkbox;
QChartView view;
QChart chart;
QLogValueAxis *xLogAxis;
QValueAxis *xLinAxis;
QValueAxis *yAxis;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(640, 480);
w.show();
return a.exec();
}
#include "main.moc"

FLTK Fl_Scrollbar Issue

I have little problem with scrollbar.
The problem is there is no separator anymore if i move the scrollbar back to the top again.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Box.H>
#define BW 200 //box width
#define BH 50 //box height
#define SEP 10 //separator
int main(){
Fl_Window win(500, 300);
Fl_Scroll *scr=new Fl_Scroll(10,10,win.w()-20,win.h()-20);
scr->color(FL_GREEN);
int col=scr->w()/BW;
int c=0,line=SEP;
for(int i=1;i<=20;i++){
Fl_Box *b = new Fl_Box(scr->x()+SEP+(c*(BW+SEP)),scr->y()+line,BW,BH);
b->box(FL_FLAT_BOX);
b->color(FL_RED);
c += 1;
if(c == col){
c = 0;
line += (BH + SEP);
}
}
new Fl_Box(SEP,scr->y()+line-SEP,SEP,SEP);
win.show();
return Fl::run();
}
Thanks.
Can't really tell what the reason is for that behaviour but
If the last Fl_Box is removed, the bottom separator disappears.
If a box is added before the loop, it doesn't lose the top line
new Fl_Box(SEP,0,SEP,SEP);

Gtkmm scroll_to() purpose

I'm wondering about this member-function's scroll_to(TextBuffer::iterator& iter, double within_margin = 0) parameter within_margin. The API says this:
The effective screen for purposes of this function is reduced by a margin of size within_margin.
...
Parameters
within_margin margin as a [0.0,0.5] fraction of screen size.
I just don't get it. What and when does this parameter modifies the behaviour? Every langugage-binding of Gtk includes the same description. I've written a small application, so you can change the passed argument to the parameter yourself.
#include <gtkmm.h>
#include <iostream>
#include <string>
using namespace std;
Gtk::TextView* text_view;
void on_add_button_clicked();
void on_scroll_button_clicked();
int main(int argc, char* argv[]) {
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
Gtk::Window window;
Glib::RefPtr<Gdk::Monitor> primary_monitor = window.get_screen()->get_display()->get_primary_monitor();
Gdk::Rectangle monitor_size;
primary_monitor->get_geometry(monitor_size);
// half-size of primary-monitor
int width = monitor_size.get_width() / 2;
int height = monitor_size.get_height() / 2;
window.set_default_size(width, height);
window.set_title(__FILE__);
Gtk::Grid grid;
grid.set_row_spacing(5);
grid.set_column_spacing(5);
Gtk::ScrolledWindow scroll_window;
text_view = new Gtk::TextView();
text_view->set_editable(true);
scroll_window.add(*text_view);
scroll_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
scroll_window.set_hexpand(true);
scroll_window.set_vexpand(true);
Glib::RefPtr<Gtk::TextBuffer> text_buffer = text_view->get_buffer();
text_buffer->set_text("Hello!\n");
text_view->set_buffer(text_buffer);
grid.attach(scroll_window, 0, 0, 2, 2);
Gtk::Button add_button("add text");
add_button.signal_clicked().connect(sigc::ptr_fun(&on_add_button_clicked));
grid.attach_next_to(add_button, scroll_window, Gtk::POS_BOTTOM, 1, 1);
Gtk::Button scroll_button("scroll to somewhere");
scroll_button.signal_clicked().connect(sigc::ptr_fun(&on_scroll_button_clicked));
grid.attach_next_to(scroll_button, add_button, Gtk::POS_RIGHT, 1, 1);
window.add(grid);
window.show_all();
return app->run(window);
}
void on_add_button_clicked() {
Glib::RefPtr<Gtk::TextBuffer> text_buffer = text_view->get_buffer();
for (int i = 0; i != 100; ++i) {
text_buffer->insert_at_cursor("foobar\n");
}
}
void on_scroll_button_clicked() {
Glib::RefPtr<Gtk::TextBuffer> text_buffer = text_view->get_buffer();
Gtk::TextBuffer::iterator it = text_buffer->end();
text_view->scroll_to(it, 0.49);
}
You can compile the code with g++ -o scroll scroll.cpp -Wall -pedantic-errors `pkg-config gtkmm-3.0 --cflags --libs` .
Thank you
If margin is 0, then scroll_to() is free to put the target anywhere in the screen. If margin is 0.45, for example, then scroll_to() will put the target in the middle 10% of the screen, if possible.
The reason you don't see this in your example, is because you are scrolling to the end iterator, and it's not possible to scroll view so that the end of the text is displayed in the middle of the screen. (Some text views include extra space after the text in order to make this possible; Gtk::TextView doesn't.)

Stop QTableView from scrolling as data is added above current position

I have a simple QTableView with a QSortFilterProxyModel and a source model of a custom TableModel subclass that inherits from QAbstractTableModel. The model is dynamically updated with additional rows.
My problem is this: If I sort the table on a column, then scroll to a specific row, and then more rows are added above this row it pushes the row down. Data is coming in fast enough that it makes it difficult to click on rows to edit them without the row changing underneath my cursor.
Is there a way to stop the table from scrolling and maintain the position of the table relative to say a selected row?
QTableView::rowViewportPosition() can be used to get the current view port position which has to be corrected if something is inserted before current index.
It can be retrieved before and after insertion of a row using signal handlers.
Thus, the scrolling can be adjusted accordingly in the signal handler after the insertion. This is done changing the value of the vertical scrollbar. (The vertical scroll mode is changed to QTableView::ScrollPerPixel to ensure correct vertical adjustment.)
A minimal code sample:
#include <iostream>
#include <QApplication>
#include <QMainWindow>
#include <QScrollBar>
#include <QStandardItemModel>
#include <QTableView>
#include <QTimer>
enum { NCols = 2 }; // number of columns
enum { Interval = 1000 }; // interval of auto action
enum { NRep = 5 }; // how often selected auto action is repeated
// fills a table model with sample data
void populate(QStandardItemModel &tblModel, bool prepend)
{
int row = tblModel.rowCount();
if (prepend) tblModel.insertRow(0);
for (int col = 0; col < NCols; ++col) {
QStandardItem *pItem = new QStandardItem(QString("row %0, col %1").arg(row).arg(col));
tblModel.setItem(prepend ? 0 : row, col, pItem);
}
}
// does some auto action
void timeout(QTimer &timer, QStandardItemModel &tblModel)
{
static int step = 0;
++step;
std::cout << "step: " << step << std::endl;
switch (step / NRep % 3) {
case 0: break; // pause
case 1: populate(tblModel, false); break; // append
case 2: populate(tblModel, true); break; // prepend
}
}
// managing the non-scrolling when something is inserted.
struct NoScrCtxt {
QTableView &tblView;
int y;
NoScrCtxt(QTableView &tblView_): tblView(tblView_) { }
void rowsAboutToBeInserted()
{
y = tblView.rowViewportPosition(tblView.currentIndex().row());
}
void rowsInserted()
{
int yNew = tblView.rowViewportPosition(tblView.currentIndex().row());
if (y != yNew) {
if (QScrollBar *pScrBar = tblView.verticalScrollBar()) {
pScrBar->setValue(pScrBar->value() + yNew - y);
}
}
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
// build some GUI
QMainWindow win;
QStandardItemModel tblModel(0, NCols);
for (int i = 0; i < 10; ++i) populate(tblModel, false);
QTableView tblView;
tblView.setVerticalScrollMode(QTableView::ScrollPerPixel);
tblView.setModel(&tblModel);
win.setCentralWidget(&tblView);
win.show();
// setup a "no-scroll manager"
NoScrCtxt ctxt(tblView);
QObject::connect(&tblModel, &QStandardItemModel::rowsAboutToBeInserted,
[&ctxt](const QModelIndex&, int, int) { ctxt.rowsAboutToBeInserted(); });
QObject::connect(&tblModel, &QStandardItemModel::rowsInserted,
[&ctxt](const QModelIndex&, int, int) { ctxt.rowsInserted(); });
// initiate some auto action
QTimer timer;
timer.setInterval(Interval); // ms
QObject::connect(&timer, &QTimer::timeout,
[&timer, &tblModel]() { timeout(timer, tblModel); });
timer.start();
// exec. application
return app.exec();
}
I compiled and tested this in Windows 10, VS2013, Qt 5.7: