I'm trying to draw with QPainter in a QPixmap, put that QPixmap in a QLabel, and that QLabel in a QScrollArea.
Sometimes the painted pixmap is bigger then my ScrollArea allows, but somehow it doesn't scroll to show the rest. What am I doing wrong?
QPixmap *pixmap = new QPixmap(10000,500);
QLabel *labeltime = new QLabel;
QHBoxLayout *layout = new QHBoxLayout;
pixmap->fill(QColor("transparent"));
int currentX = 0;
const int currentY = 220;
const int height = 50; // Coming from some static data initialization
QPainter *painter = new QPainter(pixmap);
QPen pen(Qt::gray, 2);
painter->setPen(pen);
for(int i = 0; i< viewlist.size(); i++)
{
QBrush brush(QColor(viewlist[i].color));
painter->setBrush(brush);
painter->drawRect(currentX, currentY, viewlist[i].length, height);
currentX += viewlist[i].length;
}
labeltime->setPixmap(*pixmap);
layout->addWidget(labeltime);
ui->overview->setLayout(layout);
I know this is a long and weird way to add a pixmap, but I want it to be scrollable, and I can't paint on a QScrollArea. Is there a better way? Or can someone tell me what is wrong?
Thanks
Since you only have one child widget, it is simpler to eliminate your layout. Change these lines:
layout->addWidget(labeltime);
ui->overview->setLayout(layout);
to:
ui->overview->setWidget(labeltime);
Related
I'm trying to get a custom scrolling widget in QT, and I'm getting redraw errors on scroll. Alt-tab or other redrawing events redraw correctly.
I'm basing it on the example at http://doc.qt.io/qt-5/qtwidgets-widgets-charactermap-example.html
repeatingwidget.cpp (excerpt):
QSize RepeatingWidget::sizeHint() const {
return QSize(500, itemHeight * displayItems.size() + 1);
}
void RepeatingWidget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.fillRect(event->rect(), QBrush(Qt::white));
painter.setFont(displayFont);
QRect itemRect = event->rect();
int top = itemRect.top();
QFontMetrics fontMetrics(*displayFont);
for (auto item : displayItems) {
painter.setPen(QPen(Qt::gray));
painter.drawRect(itemRect.left(), top, itemRect.right(), itemHeight);
painter.setPen(QPen(Qt::black));
painter.drawText(8, 4 + top + fontMetrics.ascent(), item.name);
top += itemHeight;
}
}
mainwindow.cpp (excerpt):
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
QMenu *filemenu = menuBar()->addMenu(tr("File"));
filemenu->addAction(tr("Quit"), this, &QWidget::close);
auto *centralWidget = new QWidget;
scrollArea = new QScrollArea;
repeatingArea = new RepeatingWidget();
scrollArea->setWidget(repeatingArea);
auto *centralLayout = new QVBoxLayout;
centralLayout->addWidget(scrollArea, 1);
centralWidget->setLayout(centralLayout);
setCentralWidget(centralWidget);
setWindowTitle(tr("Widget Test"));
}
This seems to match the example, but I'm getting redraw errors that don't happen in charmap.
I've tried setGeometry, setWidgetResizable, and different size policies, but I'm still getting these redraw errors.
After scrolling:
I don't know what I'm doing wrong because it's largely identical in important ways to the example code from the charmap.
This is the full code: https://gist.github.com/jonasbuckner/2acc1a960e457946ce4756199de3fb57
QPaintEvent is a method that allows you to make an intelligent painting, that is, to paint where necessary, thus saving resources, for example it gives us the information of the rectangle that must be painted through event->rect(), with this we can calculate the items that have to be painted since others will be hidden and therefore it is not necessary to paint them:
void RepeatingWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(event->rect(), QBrush(Qt::white));
painter.setFont(displayFont);
QFontMetrics fontMetrics(displayFont);
int i = std::max(event->rect().top()/itemHeight, 0);
int j = std::min(event->rect().bottom()/itemHeight+1, displayItems.size());
QRect itemRect(0, i*itemHeight, width(), itemHeight);
for(; i < j; i++){
painter.setPen(QPen(Qt::gray));
painter.drawRect(itemRect);
painter.setPen(QPen(Qt::black));
painter.drawText(8, 4 + itemRect.top() + fontMetrics.ascent(), displayItems[i].name);
itemRect.translate(0, itemHeight);
}
}
Your original code didn't work because you were drawing all of the items, but using the event->rect, which may only be part of the RepeatingWidget.
Sometimes it is not easy to calculate which items are in the event->rect as #eyllanesc shows. In these cases, just use clientRect instead - Qt will clip the drawing for you.
I have created a custom widget that includes a vertical layout with a qlabel(that holds an icon) and a qcombobox
and I use them as listwidget items.
They are created based on a file that have 100s of icons.
I am trying to create a slider that scales the pixmap sizes when the app is running in real time.
I can't figure out how to code this properly so I can access this property.
This is the code in the mainwindow.cpp
/*load icons*/
QDir dir (....);
QFileInfoList list = dir.entryInfoList(QDir::AllEntries |
QDir::Dirs|QDir::NoDotAndDotDot);
for(int i=0 ; i < list.length() ; i++)
{
QIcon icon;
icon.addFile(list.at(i).absoluteFilePath(), QSize(), QIcon::Normal,
QIcon::Off);
mypix = icon.pixmap(QSize(128,128));
/*Custom Widget*/
widget.push_back(new QWidget(ui->listWidget));
widget[i]->setMinimumSize(QSize(0, 150));
/*the VB with of label-combo*/
layout.push_back(new QVBoxLayout(widget[i]));
/*Qlabel that holds the icon*/
pic.push_back(new QLabel (widget[i]));
pic[i]->setPixmap(mypix);
layout[i]->addWidget(pic[i]);
box.push_back(new QComboBox(widget[i]));
box[i]->addItem(list.at(i).baseName());
layout[i]->addWidget(box[i]);
QListWidgetItem * qlistwidgetitem = new QListWidgetItem;
ui->listWidget->addItem(qlistwidgetitem);
ui->listWidget->setItemWidget(ui->listWidget->item(i),widget[i]);
}
}
QListWidgetItem has a default size that does not take into account the size of the widget, the solution is to pass the sizeHint() of the widget to QListWidgetItem, also you must not set a height of 0 to the widget, only the minimum width.
QDir dir (...);
const QFileInfoList &infolist = dir.entryInfoList(QDir::AllEntries| QDir::Dirs| QDir::NoDotAndDotDot);
for(const QFileInfo &info: infolist){
QIcon icon;
icon.addFile(info.absoluteFilePath(), QSize(), QIcon::Normal, QIcon::Off);
QPixmap pix = icon.pixmap(QSize(128,128));
QWidget *w = new QWidget(ui->listWidget);
w->setMinimumWidth(150);
QVBoxLayout *lay = new QVBoxLayout(w);
QLabel *lbl = new QLabel(w);
lbl->setPixmap(pix);
QComboBox *combo = new QComboBox(w);
combo->addItem(info.baseName());
lay->addWidget(lbl);
lay->addWidget(combo);
widget << w;
layout << lay;
box << combo;
pic << lbl;
QListWidgetItem *qlistwidgetitem = new QListWidgetItem;
qlistwidgetitem->setSizeHint(w->sizeHint());
ui->listWidget->addItem(qlistwidgetitem);
ui->listWidget->setItemWidget(qlistwidgetitem, w);
}
Update:
If you want to change the size of the icon with a QSlider it is advisable to save the icon so we can use the setData() method, then we associate a slot with the slider and in that slot we insert a new size to the icon and set it to the QLabel
for(const QFileInfo &info: infolist){
[...]
qlistwidgetitem->setData(Qt::UserRole, QVariant::fromValue(icon));
[...]
}
void Widget::on_horizontalSlider_valueChanged(int value)
{
for(int i=0; i< ui->listWidget->count(); i++){
QListWidgetItem *it = ui->listWidget->item(i);
QIcon icon = it->data(Qt::UserRole).value<QIcon>();
pic[i]->setPixmap(icon.pixmap(value, value));
QWidget *w = ui->listWidget->itemWidget(it);
it->setSizeHint(w->sizeHint());
}
}
The complete example can be found in the following link.
I have created a QDialog of size 720x480. I added 100 QLabels on it, and after that I created a QScrollArea, which has as widget the QDialog:
QDialog *window = new QDialog;
window->setWindowTitle("My Dialog");
window->setFixedSize(720, 480);
for(int i = 0; i < 100; ++i)
{
QLabel *label = new QLabel(window);
label->setText(QString::number(i));
label->move(10, i * 100);
}
QScrollArea area;
area.setWidget(window);
window->exec();
But the result is not that expected (like the vertical scrollbar to appear and to work properly ).
Your window has fixed height (to 480) and you place labels far beyond this size (last one will be placed at position 10, 9900).
You need to change your window's size.
I have got a QListWidgetItem, which has a QWidget and some QLabels. The height of the labels (imageLabel, titleLabel and descriptionLabel) varies depending on the text length. So does the height of the QWidget, which leds to different sizes in QListWidgetItem. So far the parameters for setSizeHint are static:
QListWidgetItem* listWidgetItem = new QListWidgetItem();
listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
listWidgetItem->setSizeHint(200, 180));
QWidget* widget = new QWidget();
QVBoxLayout* rootLayout = new QVBoxLayout();
rootLayout->setAlignment(Qt::AlignTop);
QHBoxLayout* contentLayout = new QHBoxLayout();
contentLayout->setAlignment(Qt::AlignLeft);
QLabel* imageLabel = new QLabel();
imageLabel->setPixmap(pixmap);
contentLayout->addWidget(imageLabel, 0, Qt::AlignTop);
QVBoxLayout* informationLayout = new QVBoxLayout();
informationLayout->setAlignment(Qt::AlignTop);
QLabel* titleLabel = new QLabel("<b>" + title + "</b>");
titleLabel->setWordWrap(true);
informationLayout->addWidget(titleLabel);
QLabel* descriptionLabel = new QLabel(description);
descriptionLabel->setWordWrap(true);
informationLayout->addWidget(descriptionLabel);
QLabel* dateLabel = new QLabel(date.toString());
informationLayout->addWidget(dateLabel);
contentLayout->addLayout(informationLayout);
rootLayout->addLayout(contentLayout);
QHBoxLayout* buttonLayout = new QHBoxLayout();
QPushButton* buttonOne = new QPushButton(tr("Button 1"));
QObject::connect(buttonOne, SIGNAL(clicked()), mButtonOneSignalMapper, SLOT(map()));
mButtonOneSignalMapper->setMapping(buttonOne, index);
buttonLayout->addWidget(buttonOne);
QPushButton* buttonTwo = new QPushButton(tr("Button 2"));
QObject::connect(buttonTwo, SIGNAL(clicked()), mButtonTwoSignalMapper, SLOT(map()));
mButtonTwoSignalMapper->setMapping(buttonTwo, index);
buttonLayout->addWidget(buttonTwo);
rootLayout->addLayout(buttonLayout);
widget->setLayout(rootLayout);
mListWidget->addItem(listWidgetItem);
mListWidget->setItemWidget(listWidgetItem, widget);
Is there any way to properly set the sizeHint regarding the width and height of the displayed content used in the QLabels of the QWidget?
For instance the first QListWidgetItem may have a descriptionLabel with a text length of 300 characters and the second QListWidgetItem may have a descriptionLabel with a text length of 1000 characters. So far both QListWidgetItems will have the same size (200px width and 180px height). While it may fit on the first QListWidgetItem, because it has only 300 characters, it may not fit on the second QListWidgetItem, because of the 1000 characters. Therefore I would like to somehow dynamically adjust the size of the QListWidgetItem regarding the needed space (first one will need less than the second one).
The way I see it, you won't be able to get a correct bounding rect for the label, unless you know it's future width, so that you can calculate the number of lines required to display the content. And you won't get the width before the layout with the other widgets is calculated.
An alternate approach might be to use an item delegate. Its sizeHint method has an option parameter with a preliminary rect, from which you can use the width and calculate the height with font metrics.
Concerning your other widgets, you could switch to a QTableWidget and put them in other columns..
The following code is not a working example .. just some clues to get you started..
class ItemDelegate : public QStyledItemDelegate
{
public:
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
painter->save();
QStyledItemDelegate::paint(painter,option,index);
QString title = index.data(Qt::UserRole).toString();
QFont f = option.font;
painter->setFont(f);
QFontMetrics fm(f);
QRect r = option.rect;
// r = r.adjusted(0, fm.lineSpacing(), 0, 0);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignTop|Qt::AlignLeft|Qt::TextWordWrap, title, &r);
painter->restore();
}
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QFont f = option.font;
QRect r = option.rect;
QFontMetrics fm(f);
QString title = index.data(Qt::UserRole).toString();
QRect br = fm.boundingRect(r,Qt::AlignTop|Qt::AlignLeft | Qt::TextWordWrap,title);
return QSize(option.rect.width(),br.height());
}
};
Hope it helps,
Johannes
I have the following problem with scrollbars in graphics view. My application takes a PDF file and creates (in some way) a QImage out of it. The QImage is then converted to QPixmap, which is used to create a QGraphicsScene and from the QGraphicsScene I create a QGraphicsView. The QGraphicsView is added to the central widget and displayed.
The code looks approximately like this
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
In the application, the view gets updated every time the PDF file changes. Now the problem is that the thing, which is in the PDF file, can also change in size and the scrollbars get messed up. I want the scrollbars after update to be in a position, such that I will see the same part of the PDF file as I saw before the update.
Can you give me some advice on how to accomplish this? I've searched for this issue, but nothing has worked in my case so far (I could have also be doing something wrong).
Thank you for your answers!
Before changing the view's contents remember scrollbar positions using view->horizontalScrollBar()->value() and view->verticalScrollBar()->value().
After changing reset previous values using setValue(int).
It's bad that you didn't provide any code that you have tried to use to accomplish this so I can't tell you what were you doing wrong.
Here is an example:
int pos_x = view->horizontalScrollBar()->value();
int pos_y = view->verticalScrollBar()->value();
pixmap_item->setPixmap(QPixmap::fromImage(new_image));
view->horizontalScrollBar()->setValue(pos_x);
view->verticalScrollBar()->setValue(pos_y);
Here pixmap_item is the stored result of scene->addPixmap(pixmap). It has QGraphicsPixmapItem* type.
I've changed the code, but it behaves strangely.
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
int hsbv = -1;
int vsbv = -1;
if (view != NULL) {
hsbv = view->horizontalScrollBar()->value();
vsbv = view->verticalScrollBar()->value();
}
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
view->horizontalScrollBar()->setVealue(hsbv);
view->verticalScrollBar()->setValue(vsbv);
When I print out the view->horizontalScrollBar()->value() at the end, I always get 83 and I get 479 for the view->verticalScrollBar()->value(), which is very wierd, but when I print hsbv and vsbv I get reasonable numbers.