I have code like this:
#include <QApplication>
#include <QGraphicsPolygonItem>
#include <QGraphicsScene>
#include <QGraphicsView>
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view;
view.resize(640, 400);
view.setScene(&scene);
auto polygon = new QGraphicsPolygonItem;
auto brush = QBrush(QColor(255, 0, 0));
polygon->setBrush(brush);
auto pen = QPen(brush, 20);
pen.setCapStyle(Qt::RoundCap);
polygon->setPen(pen);
QPolygonF polygonPath;
polygonPath << QPointF{-50, -50};
polygonPath << QPointF{100, -50};
polygonPath << QPointF{100, 100};
polygonPath << QPointF{-50, 100};
polygon->setPolygon(polygonPath);
scene.addItem(polygon);
view.show();
return app.exec();
}
Despite my round cap setting the polygon item gets rendered with straight caps.
Am I doing something obvious wrong here?
Not entirely sure but to get the result you're looking for when using a polygon path I think it's actually the 'join' style that needs to be set rather than the cap style. So change...
pen.setCapStyle(Qt::RoundCap);
to...
pen.setJoinStyle(Qt::RoundJoin);
Related
I am trying to draw only the left border in QTextDocument. Since afaik QTextFrame doesn't support selective border so I am trying with assigning a textured brush to the text frame. Something like the following -
#include <QPainter>
#include <QTextFrameFormat>
#include <QTextCursor>
#include <QTextFrame>
#include <QTextEdit>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QTextDocument doc;
QPixmap map(1024, 1024);
QPainter p;
p.begin(&map);
p.setBackground(QBrush(Qt::transparent));
p.drawRect(QRect(0,0, 4, map.height()));
p.end();
QTextFrameFormat frameFormat;
QBrush bruh(map);
bruh.setColor(Qt::transparent);
frameFormat.setBackground(bruh);
auto cur = new QTextCursor(&doc);
auto frame = cur->insertFrame(frameFormat);
auto curf = new QTextCursor(frame);
curf->insertText("Hello this is qt program!");
QTextEdit e;
e.setDocument(&doc);
e.show();
return a.exec();
}
But this prints a black background even though the background is set to be transparent(I need a transparent background with only left red border).
I can't figure out what is wrong. Also, can there be any other way to have only a left border to a QTextFrame?
Try this:
QTextDocument doc;
QPixmap map(1024, 1024);
map.fill(Qt::white);
QPainter p;
p.begin(&map);
p.fillRect(QRect(0,0, 4, map.height()),QBrush(Qt::red));
p.end();
I have created a QPolarChart and I want to hide the radial tick labels but leave the tick circles. I simply want to get rid of the text which shows "0.0", "20.0" and so on. I tried to change the label format but this did not work.
Here is a minimal example of what I tried:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPolarChart *chart = new QPolarChart();
QValueAxis *angularAxis = new QValueAxis();
angularAxis->setTickCount(13);
angularAxis->setLabelFormat("%d");
angularAxis->setRange(0, 361);
chart->addAxis(angularAxis, QPolarChart::PolarOrientationAngular);
QValueAxis *radialAxis = new QValueAxis();
radialAxis->setTickCount(10);
radialAxis->setLabelFormat(""); // <-- what do I have to add here?
radialAxis->setRange(0, 90);
chart->addAxis(radialAxis, QPolarChart::PolarOrientationRadial);
chart->legend()->setVisible(false);
QLineSeries *series = new QLineSeries();
*series << QPointF(0, 0) << QPointF(90, 22.5) << QPointF(180, 45) << QPointF(270, 67.5) << QPointF(360, 90);
chart->addSeries(series);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chart->legend()->hide();
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 400);
window.show();
return a.exec();
}
This is how the result looks like.
I want to get rid of the radial axis labels ("0.0", "10.0" ... "90.0")
The trick is to set as labelFormat to " " or an invalid format like "#":
radialAxis->setLabelFormat(" ");
# or radialAxis->setLabelFormat("#");
radialAxis->setLabelsVisible(false);
Basically, I want to know why the combination of text alignment flags and setPageSize doesn't end up with text centered in the display.
The following program does nearly exactly what I want, except that the text ends up centered only horizontally.
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow(0, Qt::FramelessWindowHint);
QGraphicsView view;
view.setAlignment(Qt::AlignLeft | Qt::AlignBottom);
view.setFrameStyle(0);
view.setBackgroundBrush(QBrush(QColor(Qt::black)));
mainWindow->setCentralWidget(&view);
QGraphicsScene scene(0, 0, 640, 480);
QGraphicsTextItem textItem;
textItem.setTextWidth(640);
textItem.document()->setPageSize(QSizeF(640, 480));
textItem.document()->setDocumentMargin(0);
textItem.document()->setDefaultTextOption(QTextOption(Qt::AlignCenter | Qt::AlignVCenter));
textItem.setDefaultTextColor(QColor(Qt::white));
textItem.setFont(QFont("monospace", 18, 63));
textItem.setHtml("Center me!");
scene.addItem(&textItem);
textItem.setVisible(true);
view.setScene(&scene);
mainWindow->show();
return a.exec();
}
I should also note that this project is constrained to Qt 4.7.1.
How do I align text both horizontally and vertically in the center of a QGraphicsView using a QGraphicsTextItem? I'm fine with a stylesheet-based solution as well.
No, it doesn't.
This bug indicates that the QTextDocument (the underlying text rendering object) specifically does not attempt to vertical alignment. After looking about, most workarounds fall into two categories. For simple (i.e. single-line plain-text) applications, implement a proxy item that renders the text in its overloaded paint method as so:
class MyProxyGraphicsItem: public QGraphicsItem {
public:
explicit MyProxyGraphicsItem(QString text, QRectF geometry, QGraphicsItem *parent=0) :
QGraphicsItem(parent), text(text), geometry(geometry)
{}
virtual ~MyProxyGraphicsItem() {}
QRectF boundingRect() const { return geometry; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawText(geometry, text, Qt::AlignCenter | Qt::AlignVCenter);
}
private:
QRectF geometry;
QString text;
}
For multi-line plain-text or rich-text (HTML), use a QLabel in stead as follows:
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow(0, Qt::FramelessWindowHint);
QGraphicsView view;
view.setAlignment(Qt::AlignLeft | Qt::AlignBottom);
view.setFrameStyle(0);
view.setBackgroundBrush(QBrush(QColor(Qt::black)));
mainWindow->setCentralWidget(&view);
QGraphicsScene scene(0, 0, 640, 480);
QLabel label("<div style=\"color:white;\">Center me!</div>");
label.setWordWrap(true);
label.setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
label.setFont(QFont("monospace", 18, 63));
scene.addWidget(&label)->setGeometry(QRectF(0,0,480,640));
view.setScene(&scene);
mainWindow->show();
return a.exec();
}
i have a fixed size QGraphicsview and my own class QGraphWidget class
GraphWidget::GraphWidget(QWidget *parent)
:QGraphicsView(parent)
{
QGraphicsScene *scene = new QGraphicsScene(this);
setScene(scene);
scale(1,-1);
setWindowTitle(tr("Poly"));
}
void GraphWidget::showPoly(){
scene()->clear();
scene()->setSceneRect(QRectF());
QPolygonF polygon;
QPen pen(Qt::black,1);
QBrush brush(Qt::black);
brush.setStyle(Qt::SolidPattern);
polygon<< QPointF(0,0)<< QPointF(10,0)<< QPointF(15,20)<<QPointF(5,10);
;
QGraphicsPolygonItem *polyItem=scene()->addPolygon(polygon,pen);
fitInView(polyItem);
}
It is possible that the scene focus the polygon and zoom it ?
I tried fitinView() or setSceneRect() but nothing worked, the polygon is still very small.
EDIT
with fitInView(polyItem->boundingRect());
The problem is that my QGraphicsview is fixed, so the fit in view has to zoom in. It can't change the size of the Qgraphicsview
So i got the answer.
The problem was that i called showPoly() inside of the constructor. But on this moment the size of the Qgraphicsview was not fixed.
I use a work around with QSingleShot
#include "mainwindow.h"
#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QTimer::singleShot(1, &w,SLOT(callShowPoly()));
return a.exec();
}
I'm trying to generate pixmaps with the following code
QList<QPixmap> pixmapList;
for (int i=0;i<50;++i){
QPixmap pixmap = QPixmap(10050,10050);
pixmap.fill(Qt::transparent);
pixmapList<<pixmap;
}
The above part works find. And I would like to paint on those pixmaps later, e.g.
QPixmap pixmap = pixmapList[10];
QPainter painter(&pixmap);
painter.drawPixmap(....); // this pixmap is 10*10
pixmapList[10]=pixmap;
or
QPainter painter(&pixmapList[10]);
painter.drawPixmap(....); // this pixmap is 10*10
but they both gave me "QPainter::begin: Paint device returned engine == 0, type: 2". May I ask the right way to paint on pixmaps in the pixmapList? Thanks very much!
Your code is OK, except that the pixmaps are too big (they occupy ~400MBytes each).
After making the pixmaps smaller, it works fine (shown for Qt 5):
#include <QGuiApplication>
#include <QPixmap>
#include <QPainter>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QList<QPixmap> pixmapList;
for (int i=0;i<50;++i){
QPixmap pixmap = QPixmap(1000,1000);
pixmap.fill(Qt::transparent);
pixmapList<<pixmap;
}
QPainter painter(&pixmapList[10]);
painter.drawLine(0, 0, 100, 100);
return 0;
}