Why is the clicked() signal for the QPieSlice not emitted? - qt

I'm using Qt charts module to draw a Nested Donuts chart just like the example in Qt Charts.
And I want every components (QPieSlice) response to the mouse event, the hovered() signals work well, however, the clicked() signals only work for the QpieSlices in the last added QPieSerie. It seems other QpieSlices do not emit the signals, since if I explicitly call the clicked() function, the slot response correctly.
This piece of code shows the issue
Widget::Widget(QWidget *parent): QWidget(parent){
QChartView *chartView = new QChartView;
QChart *chart = chartView->chart();
for (int i = 0; i < donutCount; i++) {
QPieSeries *donut = new QPieSeries;
donut->setHoleSize(minSize + i * (maxSize - minSize) / donutCount);
donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutCount);
int sliceCount = 3 + qrand() % 3;
for (int j = 0; j < sliceCount; j++) {
qreal value = 100 + qrand() % 100;
QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
slice->setLabelVisible(true);
slice->setLabelColor(Qt::white);
slice->setLabelPosition(QPieSlice::LabelInsideTangential);
connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
connect(slice, SIGNAL(clicked()), this, SLOT(selected()));
donut->append(slice);
}
m_donuts.append(donut);
chartView->chart()->addSeries(donut);
}
void Widget::selected()
{
QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
cout << slice->label().toStdString() << endl;
}
What am I doing wrong? Can someone help me?

Related

QT. graph by points in qt [duplicate]

I wrote a program in Qt, which visualizes a processed pointcloud (3D-points) by using Q3DScatter.
Now I want to add calculated keypoints with a different color.
Is that possible?
Does anyboy have some experiences with that?
Below you can see the part of code, where the point cloud is added to the data array.
QScatterDataArray * dataArray = new QScatterDataArray;
dataArray->resize(vector_seg_x->size());
QScatterDataItem * ptrToDataArray = &dataArray->first();
for(int i = 0; i < vector_seg_x->size();i++){
ptrToDataArray->setPosition(QVector3D(
(double)(iter_seg_x[i]),
(double)(iter_seg_y[i]),
(double)(iter_seg_z[i])));
ptrToDataArray++;
}
m_graph_seg->seriesList().at(0)->dataProxy()->resetArray(dataArray);
You can only set a color/gradient for a whole point list:
Q3DScatter scatter;
scatter.setFlags(scatter.flags() ^ Qt::FramelessWindowHint);
scatter.addSeries(new QScatter3DSeries);
scatter.addSeries(new QScatter3DSeries);
{
QScatterDataArray *data = new QScatterDataArray;
*data << QVector3D(0.5f, 0.5f, 0.5f) << QVector3D(-0.3f, -0.5f, -0.4f) << QVector3D(0.0f, -0.3f, 0.2f);
scatter.seriesList().at(0)->dataProxy()->resetArray(data);
QLinearGradient linearGrad(QPointF(100, 100), QPointF(200, 200));
linearGrad.setColorAt(0, Qt::blue);
linearGrad.setColorAt(1, Qt::red);
scatter.seriesList().at(0)->setBaseGradient(linearGrad);
scatter.seriesList().at(0)->setColorStyle(Q3DTheme::ColorStyle::ColorStyleObjectGradient);
}
{
QScatterDataArray *data = new QScatterDataArray;
*data << QVector3D(0.f, 0.f, 0.f);
scatter.seriesList().at(1)->dataProxy()->resetArray(data);
scatter.seriesList().at(1)->setBaseColor(Qt::green);
}
scatter.show();

QT: Generate a bar graph in a headless AMI Server. Cannot start X Display

I'm doing some processing on an Amazon Linux EC2 AWS server. When something goes wrong I need to send an email with a bar graph. I want to generate a bar graph myself, and I'm doing so by using QPainter and QImage. I'm developing an example application to simply generate the bar graph, right now. So what I do is I develop on my laptop and when I have something that works upload the code to the EC2 server and compile it there. Then run the resulting application in the server.
However, here is the hiccup. When I tried to draw text using QPainter, the program in my laptop crashes. This is because I use a QCoreApplication instead of QApplication in my main.cpp.
So I change to QApplication which fixes the problem. I upload the code to the server and compile it. Then when I try to run it, I get:
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display.
This is, of course, because the server is headless.
My question is: is there something I can install in the server that would allow me to fully use QPainter's capabilities?
Edit:
My main.cpp
#include <QApplication>
#include "../../CommonClasses/DataAnalysis/BarGrapher/bargrapher.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Setup
qreal W = 1024;
qreal H = 768;
// A fictional data set;
QList<qreal> ds1;
ds1 << 50 << 500 << 368 << 198;
QStringList descs;
descs << "Item 1";
descs << "A somewhat long and complex description";
descs << "Item 2";
descs << "Item Another";
// The actual bar graph
BarGrapher bg;
bg.configureGraphText(W,H,descs,"Some sort of label","The title of this damn thing");
BarGrapher::DataSet ds;
ds << ds1;
QImage result = bg.generateGraph(ds);
qWarning() << "ERROR" << bg.getError();
result.save("mytest.png");
qWarning() << "DONE";
return a.exec();
}
My bargrapher.cpp
#include "bargrapher.h"
const qreal BarGrapher::PROYECTION_FACTOR = 0.707106;
const qreal BarGrapher::LEFT_MARGIN = 0.1;
const qreal BarGrapher::RIGHT_MARGIN = 0.04;
const qreal BarGrapher::TOP_MARGIN = 0.08;
BarGrapher::BarGrapher()
{
}
void BarGrapher::configureGraphText(qreal width, qreal height, const QStringList &xItems, const QString &ylabel, const QString &title){
graphTitle = title;
W = width;
H = height;
itemDescriptions = xItems;
yAxisText = ylabel;
}
QImage BarGrapher::generateGraph(const DataSet &dataSet){
QImage graph(W,H,QImage::Format_RGB888);
error = "";
// Making sure that the data is coherent.
if (dataSet.isEmpty()){
error = "No data was presented";
return graph;
}
if (dataSet.size() > 2){
error = "Cannot have more than two data sets, passed " + QString::number(dataSet.size());
return graph;
}
qint32 numItems = dataSet.first().size();
for (qint32 i = 0; i < dataSet.size(); i++){
if (numItems != dataSet.at(i).size()){
error = "All data sets must be the same size: " + QString::number(numItems) + " however data set " + QString::number(i) + " has " + QString::number(dataSet.at(i).size());
return graph;
}
}
if (!itemDescriptions.isEmpty()){
if (numItems != itemDescriptions.size()){
error = "The number of item descriptios (" + QString::number(itemDescriptions.size()) + ") must coincide with the numer of points in the data set (" + QString::number(numItems) + ")";
return graph;
}
}
else{
for (qint32 i = 0; i < numItems; i++){
itemDescriptions << QString::number(i);
}
}
// The font to be used
QFont textFont("Mono");
textFont.setPixelSize(10);
//QFont titleFont("Mono",16,QFont::Bold);
// Calculating margins to get the effective graph area.
qreal XAxisLength = (1.0 - LEFT_MARGIN - RIGHT_MARGIN)*W;
qreal DW = XAxisLength/(qreal)(numItems);
// Calculating the effective graph area due to the text items.
qreal Th = 0.1*H;
qreal GH = (1.0-TOP_MARGIN)*H - Th;
qreal xOffset = LEFT_MARGIN*W;
qreal yOffset = TOP_MARGIN*H;
// --------------- Commence drawing -----------------------
QPainter painter(&graph);
// The background
painter.setBrush(QBrush(QColor(226,226,226)));
painter.drawRect(0,0,W,H);
// Drawing the axis;
QPen pen;
pen.setColor(QColor(0,0,0));
pen.setWidth(2);
painter.setPen(pen);
painter.setBrush(QBrush(Qt::black));
painter.drawLine(xOffset,yOffset+GH,xOffset + XAxisLength,yOffset+GH);
painter.drawLine(xOffset,yOffset,xOffset,yOffset+GH);
// Drawing the X Axis text
painter.setFont(textFont);
for (qint32 i = 0; i < numItems; i++){
QRectF rectF(xOffset + i*DW,yOffset+GH,DW,Th);
painter.drawText(rectF,itemDescriptions.at(i));
}
return graph;
}

Issue propagating simulated MouseEvents (Drag purpose) to parents

I'm working on a Qt/Qml application.
I'm currently trying to emulate drag behavior through Qt on drag QML elements (Listview, Flickable...) for tests purpose.
I have a specific issue that I wanted to solve with the most generic solution possible, my component is a non-interactive ListView, nested by an interactive ListView, nested itself with a MouseArea :
ListView {
anchors.fill: parent
interactive: false
ListView {
anchors.fill: parent
MouseArea {
...
}
}
}
So. My idea was : take a QML object, local coordinates (x, y) where the movement starts, find it's most nested child at position and apply the movement (dx, dy) to this child. If I'm understanding correctly how QT/QML works, it should send the event to parent if not used by the child, and Flickable components should be able to detect drags and catch them.
void xx::touchAndDrag(QObject *object, const int x, const int y, const int dx, const int dy)
{
timer = new QTimer(this);
timerIteration = 0;
deltaPoint = new QPointF(dx, dy);
parentItem = qobject_cast<QQuickItem *>(object);
item = parentItem;
startPoint = QPointF(x, y);
QPointF tempPoint = startPoint;
for( ;; ) {
//Find the most nested child at coordinate
QQuickItem* child = item->childAt(tempPoint.x(), tempPoint.y());
if(child) {
item = child;
tempPoint = child->mapFromItem(parentItem, tempPoint);
qDebug() << "child found " << item;
} else {
break;
}
}
timer->setInterval(movementDuration / nbIteration);
qDebug() << "interval " << timer->interval();
timer->setSingleShot(false);
connect(timer, SIGNAL(timeout()), this, SLOT(mouseMove()));
// Send press event at starting point
QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonPress, item->mapFromItem(parentItem, startPoint), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
qDebug() << "press " << e;
qApp->postEvent(item, e);
timer->start();
}
void xx::mouseMove()
{
timerIteration++;
QMouseEvent *e;
if(timerIteration < nbIteration) {
int x = startPoint.x() + deltaPoint->x() * timerIteration / nbIteration;
int y = startPoint.y() + deltaPoint->y() * timerIteration / nbIteration;
QPointF point = QPointF(x, y);
// Send moveEvent
e = new QMouseEvent(QEvent::MouseMove, item->mapFromItem(parentItem, point), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
qDebug() << "move! " << e ;
}else {
//End reached, send end event
QPointF point = QPointF(startPoint.x() + deltaPoint->x(), startPoint.y() + deltaPoint->y());
e = new QMouseEvent(QEvent::MouseButtonRelease, item->mapFromItem(parentItem, point), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
timer->stop();
qDebug() << "end! " << e ;
}
qApp->postEvent(item, e);
}
So... It is not working. What happens? From my tests:
-If I remove the nested child part, and give directly the interactive (to drag) QML Component (here the nested ListView), the result is good. But this is not a good solution for me, since I would have exactly to know which component should react. However, this seems to override the "interactive: false" of a component, which is a bad idea when the purpose is... to test the component.
-The mouse area receives all events. The mouseX property is updated. This is an issue. With non simulated event, MouseArea should receive the press event, some move event, then events should captured by the ListView / Flickable. Even worst, even if positions are way different (400px) between mousePress and mouseRelease events, Qt detects a MouseClicked event and triggers it on QML side...
So, not sure where to go from there. I could do some crappy child detection (checks the type of the nested child, and only accepts the good one), but I'm not very happy with it. Any idea?
Ok, so I went a bit deeper in QT code, and I understood my mistake:
To use sendEvent correctly, you have to pass them to the root view (on my project it is a QQuickView, but it can also be a QWindow). If you do not send the event on the root, it won't filter the mouseEvent as expected (through childMouseEventFilter() method)
I also discover than the MouseRelease event needs a NoButton parameter. So my code works now and looks like this:
//Set mouse timer
mouseTimer = new QTimer(this);
mouseTimer->setInterval(m_movementDuration / m_nbIteration);
mouseTimer->setSingleShot(false);
connect(mouseTimer, SIGNAL(timeout()), this, SLOT(mouseMove()));
}
void xx::pressAndDrag(QObject *object, const int x, const int y, const int dx, const int dy)
{
m_pressAndDragCompleted = false;
//Reset timer iteration
m_timerIteration = 0;
//Keep all coordinates
startPoint = ((QQuickItem *) object)->mapToScene(QPointF(x, y));
deltaPoint = new QPointF(dx, dy);
QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonPress, startPoint, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
qApp->postEvent(parent(), e);
//Start timer
mouseTimer->start();
}
void xx::mouseMove()
{
m_timerIteration++;
if (m_timerIteration < m_nbIteration + 2) {
//Move mouse
int x = startPoint.x() + deltaPoint->x() * m_timerIteration / m_nbIteration;
int y = startPoint.y() + deltaPoint->y() * m_timerIteration / m_nbIteration;
QMouseEvent *e = new QMouseEvent(QEvent::MouseMove, QPointF(x, y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
qApp->postEvent(parent(), e);
// Let some time to not trigger a flick
} else if (m_timerIteration - m_nbIteration >= 400 / mouseTimer->interval()) {
//End movement
QPointF point = QPointF(startPoint.x() + deltaPoint->x(), startPoint.y() + deltaPoint->y());
QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonRelease, point, Qt::NoButton, Qt::NoButton, Qt::NoModifier );
qApp->postEvent(parent(), e);
//Stop timer
mouseTimer->stop();
m_pressAndDragCompleted = true;
}
}
If it can help. :)

Taking screenshot of view/layout programmatically in BlackBerry10

is it possible to take a screenshot of a container like DockLayout and all of its children programmatically in BB10 (native)? I've found the Screenshot class in the docs, but it's only possible to take a display or app window screenshot...
Any tips or hints?
Thanks.
This is the basic code I use for capturing an area of the screen under program control.
Note that you can only capture a screen displayed by the program this code is in.
void ScreenCapture::capture(qreal x, qreal y, qreal width, qreal height)
{
smDebug(1) << "Capturing" << x << y << width << height;
screen_pixmap_t screen_pix;
screen_buffer_t screenshot_buf;
screen_context_t context;
char *screenshot_ptr = NULL;
int screenshot_stride = 0;
int usage, format;
int size[2];
screen_create_context(&context, 0);
screen_create_pixmap(&screen_pix, context);
usage = SCREEN_USAGE_READ | SCREEN_USAGE_NATIVE;
screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_USAGE, &usage);
format = SCREEN_FORMAT_RGBA8888;
screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_FORMAT, &format);
size[0] = 0;
size[1] = 0;
screen_get_window_property_iv(Application::instance()->mainWindow()->handle(), SCREEN_PROPERTY_SIZE, size);
smDebug(1) << size[0] << 'x' << size[1];
screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_BUFFER_SIZE, size);
screen_create_pixmap_buffer(screen_pix);
screen_get_pixmap_property_pv(screen_pix, SCREEN_PROPERTY_RENDER_BUFFERS,
(void**)&screenshot_buf);
screen_get_buffer_property_pv(screenshot_buf, SCREEN_PROPERTY_POINTER,
(void**)&screenshot_ptr);
screen_get_buffer_property_iv(screenshot_buf, SCREEN_PROPERTY_STRIDE,
&screenshot_stride);
screen_read_window(Application::instance()->mainWindow()->handle(), screenshot_buf, 0, NULL ,0);
QByteArray array;
int nbytes = size[0] * size[1] * 4;
write_bitmap_header(nbytes, array, size);
for (int i = 0; i < size[1]; i++)
{
array.append(screenshot_ptr + i * screenshot_stride, size[0] * 4);
}
QImage image = QImage::fromData(array, "BMP");
image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
baseImage = QImage(width,height,QImage::Format_ARGB32_Premultiplied);
baseImage.painter().drawImage(0, 0, image, x, y, width, height);
if (mWaterMarkImage != NULL)
baseImage.updateToView(mWaterMarkImage);
soundplayer_play_sound("event_camera_shutter");
screen_destroy_pixmap(screen_pix);
smDebug(1) << "capture done.";
}

Get error messages when using QPrinter, QPainter and QTextDocument together

In my Qt application, I want to create a preview page with the content that contains the Header, Footer title and a TableView.
This is the code I used:
void MainWindow::print(QPrinter *printer)
{
int xscale = 50;
int yscale = 30;
QPoint top_left = QPoint(xscale, yscale);
QPoint top_right = QPoint(xscale + 552, yscale + 20);
QPoint bottom_left = QPoint(xscale, yscale + 1020);
QPoint bottom_right = QPoint(xscale + 492, yscale + 1020);
QPainter painter(printer);
painter.setRenderHints(QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::SmoothPixmapTransform, true);
// Header
painter.setFont(QFont("Arial", 10));
painter.drawImage(top_left, QImage(":/images/images/logo.png"));
painter.drawText(top_right, "Header");
// Print the Table
QString strStream;
QTextStream out(&strStream);
out << "<html>\n"
"<head>\n"
"<meta content=\"text/html; charset=utf-8\">\n"
"<title>Demo MyTableView</title>\n"
"<style tyle=\"text/css\">th{font-size: 14pt}\n td{font-size: 12pt}\n table td + td + td + td{font-weight:bold}</style>"
"</head>\n"
"<body bgcolor=#ffffff link=#5000A0>\n"
"<table cellspacing=\"0\" cellpadding=\"2\" border=\"1\" width=\"100%\">\n";
// Print the headers
out << "<thead><tr bgcolor=\"#ffffff\">";
for (int column = 0; column < columnCount; column++)
if (!myTableView->isColumnHidden(column))
out << QString("<th>%1</th>").arg(myTableView->model()->headerData(column, Qt::Horizontal).toString());
out << "</tr></thead>\n";
// Print the data
for (int row = 0; row < rowCount; row++) {
out << "<tr>";
for (int column = 0; column < columnCount; column++) {
if (!myTableView->isColumnHidden(column)) {
QString data = myTableView->model()->data(myTableView->model()->index(row, column)).toString().simplified();
out << QString("<td bkcolor=0 align=center>%1</td>").arg((!data.isEmpty()) ? data : QString(" "));
}
}
out << "</tr>\n";
}
out << "</table>\n"
"</body>\n"
"</html>\n";
QTextDocument *document = new QTextDocument();
document->setHtml(strStream);
document->print(printer); // I got the error messages at here
delete document;
// Footer
painter.setFont(QFont("Arial", 10));
painter.drawText(bottom_left, "Copyright 2013");
// Get current date and time
QDateTime dateTime = QDateTime::currentDateTime();
QString dateTimeString = dateTime.toString();
painter.drawText(bottom_right, dateTimeString);
}
When I run the application, I only see Header and Footer title in the preview page, the TableView is not shown. Then I used qDebug() to check and I got the error messages
QPrinter::setDocName: Cannot be changed while printer is active
QPainter::begin: A paint device can only be painted by one painter at a time.
at the line
document->print(printer);
How can I solve this issue to print the data normally with the Header, Footer title and TableView?
Thanks for your help!
Well, if Qt complains about using multiple painters at a time, make it use only one:) In other words, just split the code in your MainWindow::print() function into the smaller routines for each part of your document:
void MainWindow::drawHeader(QPrinter *printer)
{
QPainter painter(printer);
// .. Draw the header
[..]
}
void MainWindow::drawFooter(QPrinter *printer)
{
QPainter painter(printer);
painter.setFont(QFont("Arial", 10));
painter.drawText(bottom_left, "Copyright 2013");
[..]
}
void MainWindow::drawTable(QPrinter *printer)
{
QTextDocument document;
document.print(printer);
[..]
}
And finally:
void MainWindow::print(QPrinter *printer)
{
// Init something.
drawHeader(printer);
drawTable(printer);
drawFooter(printer);
[..]
}

Resources