Ok, so I want to customize the height and the position of icons of the tabs from a QToolBox widget. I am not able to do it using stylesheet (I tried different options, but none worked, see question: Customizing QToolBox: tab height), so I decided to give it a try to using a proxy. I know there are the controlElements:
CE_ToolBar
CE_ToolBoxTabShape
CE_ToolBoxTabLabel
And so I can use them on the drawControl function to get what I want.
void drawControl(ControlElement oCtrElement,
const QStyleOption *poStyleOptionption,
QPainter *poPainter,
const QWidget *poWidget) const
But I am not sure how to implement it... Any help or example?
Thanks,
UPDATE:
I have created the proxy, assigned it to the qtoolbox and added conditions to see if I could do something:
...
ToolBoxProxy* tabStyle = new ToolBoxProxy();
ui->toolBox->setStyle(tabStyle);
...
toolboxproxy.h
#include <QProxyStyle>
#include <QPainter>
class ToolBoxProxy : public QProxyStyle
{
public:
explicit ToolBoxProxy();
void drawControl(ControlElement oCtrElement, const QStyleOption * poStylrOptionption, QPainter * poPainter, const QWidget * poWidget = 0) const;
};
toolboxproxy.cpp
#include "toolboxproxy.h"
#include <QDebug>
ToolBoxProxy::ToolBoxProxy(){
}
void ToolBoxProxy::drawControl(ControlElement oCtrElement, const QStyleOption *poStyleOptionption, QPainter *poPainter, const QWidget *poWidget) const
{
if (oCtrElement == CE_ToolBar) {
qDebug() << "CE_ToolBar";
} else if (oCtrElement == CE_ToolBoxTabShape) {
qDebug() << "CE_TOOLBOXTABSHAPE";
} else if (oCtrElement == CE_ToolBoxTabLabel) {
qDebug() << "CE_ToolBoxTabLabel";
}
QProxyStyle::drawControl(oCtrElement, poStyleOptionption, poPainter, poWidget);
}
I don't know why, but the software never prints out any of the 3 qDebug()... but if I set a breakpoint, it only reaches the function when the control element is any of those:
CE_ItemViewItem
CE_ShapedFrame
CE_ToolButtonLabel
Related
I have a QHeaderView that does a few things when clicking either to the left or right of the center of a section header.
For this, I need to know the position of the click in relation to the (possibly scrolled) content of the QHeaderView's viewport.
The actual click position, however, refers only to the QHeaderView (which is always fixed).
I tried variants of mapTo/From, but can't find the correct way to do it.
Here's simplified code:
void MyTableHeader::headerSectionClicked(int section_index, int click_pos)
{
int section_size = sectionSize(0); // All sections in a header are equally sized
int section_center = (section_size * (section_index+ 1)) - (section_size / 2); // Center of the clicked section
if (section_index>= 0)// if mouse is over an item
{
if (orientation() == Qt::Horizontal)
{
QPoint x_pos = QPoint(click_pos, 0);
int mapped_offset = viewport()->mapFrom(this, x_pos).x();
if (mapped_offset != -1)
{
// If the click was to the right of the center, iterate on the index
if (mapped_offset >= section_center)
{
section_index++;
}
}
}
else
{
// Same thing for the Y-dimension
}
}
// Neat stuff after this
}
The part where the problem occurs, is where I want to find out on which side of the section the click happened.
// If the click was to the right of the center, iterate on the index
if (mapped_offset >= section_center)
{
in_index++;
}
This mapped_offset does not properly refer to the same context as the section center.
The following solution might give you an idea on what to do.
MyHeaderView.h
#pragma once
#include <QHeaderView>
class MyHeaderView : public QHeaderView
{
Q_OBJECT
public:
MyHeaderView(Qt::Orientation orientation, QWidget* parent = nullptr);
};
MyHeaderView.cpp
#include "MyHeaderView.h"
#include <QDebug>
MyHeaderView::MyHeaderView(Qt::Orientation orientation, QWidget* parent) : QHeaderView(orientation, parent)
{
setSectionsClickable(true);
connect(this, &MyHeaderView::sectionClicked, [this](int section)
{
QRect currentSectionRect;
currentSectionRect.setRect(sectionViewportPosition(section), 0, sectionSize(section), viewport()->height());
auto pos = QCursor::pos();
auto localPos = mapFromGlobal(pos);
qDebug() << currentSectionRect << localPos;
qDebug() << currentSectionRect.contains(localPos); //"Always true!"
});
}
main.cpp
#include "MyHeaderView.h"
#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>
int main(int argc, char **args)
{
QApplication app(argc, args);
auto model = new QStandardItemModel;
auto view = new QTableView;
view->setModel(model);
model->setRowCount(4);
model->setColumnCount(4);
view->setHorizontalHeader(new MyHeaderView(Qt::Horizontal));
view->show();
app.exec();
}
One of my earlier Question deals with a customized QStyledItemDelegate containing a QWidget with two QLabels side by side.
I was really satisfied with the easy solution given by Joseph Ireland. Unfortunately, the given is solution is broken, but I didn't realized this immediately. If the QTableWidget containing my QStyledItemDelegate gets too small, the scrollbars are activated.
Now scrolling destroys the correct drawing of my cell elements. This seems to be some kind of update problem. I realized this, after I draw a rectangle around the viewport region of my QAbstractScollArea.
My table looks like the following, if you will scroll violently:
Cells contents are not drawn at the right place or seemingly not draw after all. Happens strangely for every second row. Even more the rectangle drawn around the viewport region of my QAbstractScrollArea is messed up. If the window is redrawn (hide/show window) everything is fine.
What might the solution to this kind of update/repaint problem? Maybe I need to repaint after scrolling is finished?
My adapted solution posted by Joseph Ireland was:
Header-File: TwoNumbersDelegate.h
#pragma once
#include <QStyledItemDelegate>
class QLabel;
class TwoNumbersDelegate : public QStyledItemDelegate {
public:
TwoNumbersDelegate(QObject* parent = nullptr);
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
private:
QLabel* mLeft;
QLabel* mRight;
QFrame* mFrame;
};
Source File: TwoNumbersDelegate.cpp
#include "TwoNumbersDelegate.h"
#include <QLabel>
#include <QPainter>
#include <QDebug>
#include <QHBoxLayout>
#include <QTableWidget>
TwoNumbersDelegate::TwoNumbersDelegate(QObject* parent /*= nullptr*/) : QStyledItemDelegate(parent)
{
mLeft = new QLabel("%1");
mRight = new QLabel("%2");
mFrame = new QFrame;
mFrame->setLayout(new QHBoxLayout);
mFrame->layout()->addWidget(mLeft);
// you could add a spacer here maybe
mFrame->layout()->addWidget(mRight);
mFrame->setAttribute(Qt::WA_DontShowOnScreen, true);
mFrame->show();
}
void TwoNumbersDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
auto data = index.data(Qt::EditRole);
auto list = data.toList();
if (list.size() != 2) {
QStyledItemDelegate::paint(painter, option, index);
}
mLeft->setText(list.at(0).toString());
mRight->setText(list.at(1).toString());
mFrame->resize(option.rect.size());
qDebug() << option.rect.size();
// mFrame->layout()->invalidate();
// mFrame->layout()->activate();
if (auto tableWidget = qobject_cast<QTableWidget*>(parent())) {
auto cellRegion = QRegion(option.rect);
auto viewportRegion = QRegion(tableWidget->viewport()->rect());
auto intersectedRegion = cellRegion.intersected(viewportRegion);
intersectedRegion.translate(-option.rect.topLeft());
painter->drawRect(tableWidget->viewport()->rect().adjusted(0,0,-1,-1));
painter->save();
painter->translate(option.rect.topLeft());
mFrame->render(painter, QPoint(), intersectedRegion, QWidget::DrawChildren);
qDebug() << cellRegion << viewportRegion << intersectedRegion;
painter->restore();
}
}
QSize TwoNumbersDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return mFrame->minimumSizeHint();
}
The following program served me as a test runner:
#include <QApplication>
#include <QTableWidget>
#include <QVBoxLayout>
#include "TwoNumbersDelegate.h"
int main(int argc, char** args) {
QApplication q(argc, args);
auto frame = new QFrame;
frame->setLayout(new QVBoxLayout);
//frame->setStyleSheet("QFrame { background: green}"); // Activate this to see overdrawing
auto table = new QTableWidget;
table->setAlternatingRowColors(true);
table->setItemDelegate(new TwoNumbersDelegate);
table->setRowCount(20);
table->setColumnCount(10);
for (auto iter = 0; iter < 20; iter++) {
for (auto colIter = 0; colIter < 10; colIter++) {
auto item = new QTableWidgetItem;
QVariantList map;
map << iter << iter*colIter;
item->setData(Qt::EditRole, map);
table->setItem(iter, colIter, item);
}
}
frame->layout()->addWidget(table);
frame->show();
q.exec();
}
I have a QGraphicsSvgItem subclass where I would like to modify the svg color. I want to use for that QGraphicsColorizeEffect, and it works great.
My items also have a custom selection rectangle, highlighted - similar to other item types.
When I apply the colorize effect, the highlight also turns to the same color...
I have tried to setEnabled(false); in paint but it seems to have no effect.
sample code:
file mysvg.h
#ifndef MYSVG_H
#define MYSVG_H
#include <QGraphicsSvgItem>
#include <QGraphicsColorizeEffect>
class MySvg : public QGraphicsSvgItem
{
public:
MySvg();
~MySvg();
virtual void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget = NULL);
private:
QGraphicsColorizeEffect* m_effect;
void drawSelectionRectangle(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRectF& rectangle);
};
#endif // MYSVG_H
file mysvg.cpp
#include <QStyleOptionGraphicsItem>
#include <QStyle>
#include <QPainterPath>
#include <QPainter>
#include <QFileDialog>
#include <QSvgRenderer>
MySvg::MySvg()
{
m_effect = new QGraphicsColorizeEffect();
m_effect->setColor(Qt::red);
setGraphicsEffect(m_effect);
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
QString filename = QFileDialog::getOpenFileName(0, tr("Open Svg File"),
QString(), tr("Svg files (*.svg *.svgz)"));
setSharedRenderer(new QSvgRenderer(filename));
}
MySvg::~MySvg()
{
delete renderer();
delete m_effect;
}
void MySvg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QStyleOptionGraphicsItem opt(*option);
opt.state = QStyle::State_None;
QGraphicsSvgItem::paint(painter, &opt, widget);
//m_effect->setEnabled(false); // no effect though seemed logical
QRectF rectangle = boundingRect();
if (option->state & (QStyle::State_Selected))
drawSelectionRectangle(painter, option, rectangle);
//m_effect->setEnabled(true);
}
void MySvg::drawSelectionRectangle(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRectF &rectangle)
{
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine));
painter->setBrush(QColor(255, 188, 0, 50));
painter->drawRect(rectangle);
}
file main.cpp
#include <QApplication>
#include <QGraphicsView>
#include "mysvg.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene s;
QGraphicsView view;
view.setScene(&s);
s.setSceneRect(-50, -50, 500, 650);
view.show();
MySvg* svg = new MySvg();
s.addItem(svg);
return app.exec();
}
file mysvg.pro
QT += core gui svg
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = mysvg
TEMPLATE = app
SOURCES += main.cpp \
mysvg.cpp
HEADERS += mysvg.h
I have considered making a QGraphicsSvgItem a private member of the MySvg item - but the MySvg item has to do a lot of other things, and I don't know what to do with the renderer (who would own it...) - if I can figure out how to make a QGraphicsSvgItem subclass a member of the MySvg class, I can apply the colorize to the member and perform all other operations on the MySvg item...
Please help me figure a way to apply color to the svg, but not other drawing portion of the item.
Edit - I have tried to add a member item to the class and apply the colorize effect to the member - but it doesn't apply the colorize effect at all... The svg loads with all original colors.
Here is the code containing a member item:
new mysvg.h
class SvgMember : public QGraphicsSvgItem
{
public:
SvgMember (const QByteArray &content, const QColor& c);
~SvgMember ();
private:
QGraphicsColorizeEffect* m_effect;
};
class MySvg : public QGraphicsItem
{
public:
MySvg();
~MySvg();
virtual void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget = NULL);
virtual QRectF boundingRect() const;
virtual QPainterPath shape() const;
private:
void drawSelectionRectangle(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRectF& rectangle);
SvgMember * m_member;
};
new mysvg.cpp
MySvg::MySvg()
{
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
QString filename = QFileDialog::getOpenFileName(0, QObject::tr("Open Svg File"),
QString(), QObject::tr("Svg files (*.svg *.svgz)"));
QFile f(filename);
f.open(QFile::ReadOnly | QFile::Text);
QByteArray svgContents = f.readAll();
f.close();
m_member = new SvgMember (svgContents, Qt::red);
}
MySvg::~MySvg()
{
delete m_member;
}
void MySvg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QStyleOptionGraphicsItem opt(*option);
opt.state = QStyle::State_None;
m_member->paint(painter, &opt, widget);
QRectF rectangle = boundingRect();
if (option->state & (QStyle::State_Selected))
drawSelectionRectangle(painter, option, rectangle);
}
/*! \brief reimplemented to use member rectangle */
QRectF MySvg::boundingRect() const
{
return m_member->boundingRect();
}
/*! \brief reimplemented to use member shape */
QPainterPath MySvg::shape() const
{
return m_member->shape();
}
void MySvg::drawSelectionRectangle(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRectF &rectangle)
{
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine));
painter->setBrush(QColor(255, 188, 0, 50));
painter->drawRect(rectangle);
}
SvgMember ::SvgMember (const QByteArray &content, const QColor &c)
{
m_effect = new QGraphicsColorizeEffect();
setGraphicsEffect(m_effect);
m_effect->setColor(c);
setSharedRenderer(new QSvgRenderer(content));
}
SvgMember ::~SvgMember ()
{
delete renderer();
delete m_effect;
}
What can I do to apply the colorize effect to the svg - but not to the selection rectangle ?
The effect is a feature of the base class QGraphicsItem. It is applied to the entire graphics item and all its children. So, everything that is painted inside the item is affected by its effect.
The selection rectangle should be painted outside of the SVG item object.
It can be achieved by enclosing QGraphicsSvgItem by the composite class QGraphicsItemGroup.
When a QGraphicsItem is added to QGraphicsItemGroup it becomes reparented. Thus the item is destroyed when the group object is destroyed. So, it is not needed to delete it manually. QGraphicsItem takes ownership of effect, so it is not needed to delete the effect object.
The following class MyGraphicsItemGroup works as you expect.
Implementation "mygraphicsitemgroup.cpp"
#include "mygraphicsitemgroup.h"
#include <QGraphicsColorizeEffect>
#include <QGraphicsSvgItem>
#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <QFileDialog>
MyGraphicsItemGroup::MyGraphicsItemGroup()
{
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
QString filename = QFileDialog::getOpenFileName(0,
QObject::tr("Open Svg File"), QString(),
QObject::tr("Svg files (*.svg *.svgz)"));
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect();
effect->setColor(Qt::red);
QGraphicsSvgItem *svg = new QGraphicsSvgItem(filename);
svg->setGraphicsEffect(effect);
addToGroup(svg);
}
void MyGraphicsItemGroup::paint(QPainter* painter,
const QStyleOptionGraphicsItem* option, QWidget* widget)
{
QStyleOptionGraphicsItem opt(*option);
opt.state = QStyle::State_None;
QGraphicsItemGroup::paint(painter, &opt, widget);
QRectF rectangle = boundingRect();
if (option->state & QStyle::State_Selected)
drawSelectionRectangle(painter, option, rectangle);
}
void MyGraphicsItemGroup::drawSelectionRectangle(QPainter *painter,
const QStyleOptionGraphicsItem *option, const QRectF &rectangle)
{
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine));
painter->setBrush(QColor(255, 188, 0, 50));
painter->drawRect(rectangle);
}
Header "mygraphicsitemgroup.h"
#ifndef MYGRAPHICSITEMGROUP_H
#define MYGRAPHICSITEMGROUP_H
#include <QGraphicsItemGroup>
class MyGraphicsItemGroup : public QGraphicsItemGroup
{
public:
MyGraphicsItemGroup();
virtual void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option, QWidget* widget);
void drawSelectionRectangle(QPainter *painter,
const QStyleOptionGraphicsItem *option, const QRectF &rectangle);
};
#endif // MYGRAPHICSITEMGROUP_H
I want to indent the title of a QDockWidget w/o adding spaces.
#include <QStyle>
#include <QProxyStyle>
#include <iostream>
class my_style : public QProxyStyle
{
Q_OBJECT
public:
my_style (QStyle* style = 0) :
QProxyStyle (style)
{
}
virtual ~my_style ()
{
}
virtual QRect subElementRect (SubElement element, const QStyleOption * option, const QWidget * widget = 0) const
{
QRect rect = QProxyStyle::subElementRect (element, option, widget);
if (element == QStyle::SE_DockWidgetTitleBarText)
{
rect.adjust (50, 0, 0, 0);
}
//std::cerr << "debug:" << element << std::endl;
return rect;
}
};
I have no clue why but when I apply my style it's never running into that if. If I debug the method I only get an output for two different elements which are the buttons in the title bar.
subElementRect is not called to get the title area for all styles. At least, XP, Vista and MacOSX styles are using directly QStyleOption::rect which is passed as parameter to the drawControl function for CE_DockWidgetTitle.
To handle both cases, you should also reimplement drawControl:
void drawControl(ControlElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const
{
const QStyleOptionDockWidget *dockWidget;
if(element == CE_DockWidgetTitle &&
(dockWidget = qstyleoption_cast<const QStyleOptionDockWidget *>(option)))
{
QStyleOptionDockWidget copy = *dockWidget;
copy.rect.adjust(50,0,0,0);
// or you can add spaces in the title to avoid the border moving left too
// copy.title = QString(50 / copy.fontMetrics.width(" "), QChar(' ')) + copy.title;
QProxyStyle::drawControl(element, ©, painter, widget);
return;
}
QProxyStyle::drawControl(element, option, painter, widget);
}
Alternatively you could use a style sheet, with a padding or a margin:
dockWidget->setStyleSheet("::title { position: relative; padding-left: 50px;"
" text-align: left center }");
The "position" rule does nothing, but is necessary, because strangely the style is only applied if some other rule categories are also present.
The text needs to be vertically realigned too because the alignment seem to be lost when using a style sheet.
I am developing a GUI application in Qt, and I have some difficulties embedding a custom widget in my ui. From Qt's documentation I can see it is possible to promote such a widget. However, I am still a little confused about how this should be done.
My widget QTreeWidget is heavily inspired by Qt's torrent example, where I want to embed this in my application:
So I have for my FilesView class (not included the src code, because it is trivial):
#include <QTreeWidget>
#include <QUrl>
#include <QFile>
#include <QDragMoveEvent>
#include <QDropEvent>
// FilesView extends QTreeWidget to allow drag and drop.
class FilesView : public QTreeWidget
{
Q_OBJECT
public:
FilesView(QWidget *parent = 0);
signals:
void fileDropped(const QString &fileName);
protected:
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
};
To this is a TorrentViewDelegate class (comment the progressbar for testing purposes)
#include <QItemDelegate>
#include <QMainWindow>
#include <QApplication>
// TorrentViewDelegate is used to draw the progress bars.
class TorrentViewDelegate : public QItemDelegate
{
Q_OBJECT
public:
inline TorrentViewDelegate(QMainWindow *mainWindow) : QItemDelegate(mainWindow) {}
inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index ) const
{
if (index.column() != 2) {
QItemDelegate::paint(painter, option, index);
return;
}
// Set up a QStyleOptionProgressBar to precisely mimic the
// environment of a progress bar.
QStyleOptionProgressBar progressBarOption;
progressBarOption.state = QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.rect = option.rect;
progressBarOption.fontMetrics = QApplication::fontMetrics();
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
// Set the progress and text values of the style option.
//int progress = qobject_cast<MainWindow *>(parent())->clientForRow(index.row())->progress();
int progress = 40;
progressBarOption.progress = progress < 0 ? 0 : progress;
progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);
// Draw the progress bar onto the view.
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
}
};
In the example the embed the widget in MainWindow as:
filesView = new FilesView(this);
filesView->setItemDelegate(new TorrentViewDelegate(this));
filesView->setHeaderLabels(headers);
filesView->setSelectionBehavior(QAbstractItemView::SelectRows);
filesView->setAlternatingRowColors(true);
filesView->setRootIsDecorated(false);
ui->verticalLayout_Filebox->addWidget(filesView);
How can I do this from the Qt designer?
Place an empty widget where you want to have your FilesView
Right click on it and select Promote to
Set the promoted class name to FilesView press Add and then Promote
You cannot set the delegate from QtDesigner
For more info have a look here:
http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html
The second option you have is to create a plugin for your widget which will allow you to set its properties through designer. If you are not going to use your widget multiple times I do not suggest it. For more details check the following link:
http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html