QTextDocumentWriter removes carriage return translation to tables when creating a document - qt

When I create a document using QTextDocumentWriter in the "odf" format, line wrapping in the table does not work. When I create an "html" document, line wrapping in the table works. The "odf" format is required for older versions of the open office. In older versions, the office version "HTML" does not open.
How to make a row transfer to a table in "odf" format?
Attached the test code:
#include <QString>
#include <QTextStream>
#include <QTextDocument>
#include <QTextDocumentWriter>
#include <QTextCursor>
#include <QTextBlock>
#include <QDate>
#include <QTextTableCell>
void createsOdfWithTable(){
QTextDocument *doc = new QTextDocument;
doc->setDocumentMargin(10);
QTextCursor cursor(doc);
cursor.movePosition(QTextCursor::Start);
QTextCharFormat textFormat;
textFormat.setFontPointSize(10);
QTextCharFormat boldFormat;
boldFormat.setFontWeight(QFont::Bold);
//HEADERS
QTextCharFormat header1Format = boldFormat;
header1Format.setFontPointSize(12);
header1Format.setFontUnderline(true);
header1Format.setForeground(QBrush(QColor("#0000FF")));
QTextCharFormat header2Format = header1Format;
header2Format.setFontPointSize(14);
QTextCharFormat header3Format = header1Format;
header3Format.setFontPointSize(16);
QTextCharFormat titleFormat = boldFormat;
titleFormat.setFontPointSize(20);
header1Format.setForeground(QBrush(QColor("#0000FF")));
QTextBlockFormat blockFormat = cursor.block().blockFormat();
QTextBlockFormat titleBlockFormat = cursor.block().blockFormat();
titleBlockFormat.setAlignment(Qt::AlignHCenter);
QTextCharFormat alternateCellFormat = textFormat;
alternateCellFormat.setBackground(QBrush(QColor("#D1EBFF")));
QTextTableFormat tableFormat;
tableFormat.setBorder(1);
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
tableFormat.setCellPadding(2);
tableFormat.setCellSpacing(0);
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
tableFormat.setAlignment(Qt::AlignLeft);
//Title of the document
cursor.insertBlock();
cursor.setBlockCharFormat(titleFormat);
cursor.setBlockFormat(titleBlockFormat);
cursor.insertText(QObject::tr("My Great Document !"));
cursor.insertBlock(); //new line
cursor.insertBlock();
cursor.insertBlock();
cursor.setBlockFormat(blockFormat);
cursor.insertText(QObject::tr("Header 1"), header1Format);
cursor.insertBlock();
cursor.setCharFormat(textFormat);
cursor.insertText("Author:\tMe");
cursor.insertBlock();
cursor.insertText("Version:\tMe");
cursor.insertBlock();
cursor.insertText("Date:\t" + QDate::currentDate().toString("dd.MM.yyyy"));
cursor.insertBlock();
cursor.insertText(QObject::tr("Header 2"), header2Format);
cursor.insertBlock();
cursor.insertText(QObject::tr("Header 3"), header3Format);
cursor.insertBlock();
//Insert table with nb row = 10, column = 2
QTextTable *table = cursor.insertTable(10+1, 2, tableFormat);
QTextTableCell headerCell = table->cellAt(0, 0);
QTextCursor headerCellCursor = headerCell.firstCursorPosition();
headerCellCursor.insertText(QObject::tr("Name"), boldFormat);
headerCell = table->cellAt(0, 1);
headerCellCursor = headerCell.firstCursorPosition();
headerCellCursor.insertText(QObject::tr("Value"), boldFormat);
for(int i = 0; i < 10; i++){
QTextCharFormat cellFormat = i % 2 == 0 ? textFormat : alternateCellFormat;
QTextTableCell cell = table->cellAt(i + 1, 0);
cell.setFormat(cellFormat);
QTextCursor cellCursor = cell.firstCursorPosition();
cellCursor.insertText("Row \n" + QString::number(i));
cell = table->cellAt(i + 1, 1);
cell.setFormat(cellFormat);
cellCursor = cell.firstCursorPosition();
cellCursor.insertText(QString::number(i));
}
cursor.movePosition(QTextCursor::End);
QTextDocumentWriter writer("test3.odt");
writer.setFormat("odf");
writer.write(doc);
QTextDocumentWriter writer2("test4.odt");
writer2.setFormat("html");
writer2.write(doc);
delete doc;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
createsOdfWithTable();
return a.exec();
}
ODF
HTML

Related

PCL addpointcloud interface in debug mode normal operation release mode error

I am a newbie in point cloud library and currently I have a problem like the title.I've been debugging in debug mode before, but now I need to release so I'm debugging in release mode for the first time.When I get an error when I load the point cloud using the code below
viewer->removeAllPointClouds();
viewer->removeAllShapes();
viewer->addPointCloud<pcl::PointXYZRGB>(temcloud_ptr,"point cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
viewer->resetCamera();
In viewer->addpointcloud<pcl::PointXYZRGB>(temcloud_ptr,"point cloud");this code,will occur this error :An exception was raised: a read access permission conflict.this is 0xFFFFFFFFFFFFFFDF. and stop here code from xstring screen .
_CONSTEXPR20_CONTAINER bool _Large_string_engaged() const noexcept {
#ifdef __cpp_lib_constexpr_string
if (_STD is_constant_evaluated()) {
return true;
}
#endif // __cpp_lib_constexpr_string
return _BUF_SIZE <= _Myres;
Here's my code
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
auto renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
renderWindow->AddRenderer(renderer);
viewer.reset(new pcl::visualization::PCLVisualizer(renderer,
renderWindow,
"viewer",
false));
ui.qvtkwidget->setRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui.qvtkwidget->interactor(),
ui.qvtkwidget->renderWindow());
viewer->setBackgroundColor(0.8941176, 1, 0.9137254);
cloud_ptr.reset(new pcl::PointCloud<PointXYZRGBI>);
cloudrgb_ptr.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
cloudi_ptr.reset(new pcl::PointCloud<pcl::PointXYZI>);
}
void MainWindow::openFile()
{
QString FileName = QFileDialog::getOpenFileName(this,
tr("Open Resource"),
".",
tr("File1(*.las);;File2(*.txt);;File3(*.pcd);;File4(*.ply);;File5(*.obj)"));
if (FileName.isEmpty())
return;
const std::string sstr = FileName.toLocal8Bit();
if (FileName != "")
{
Openfile of;
if (FileName.endsWith("las"))
{
const char* cc_FileName = sstr.c_str();
std::ifstream ifs;
ifs.open(cc_FileName, std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
liblas::Header const& header = reader.GetHeader();
int pointsNum = header.GetPointRecordsCount();
cloudrgb_ptr->width = pointsNum;
cloudrgb_ptr->height = 1;
cloudrgb_ptr->resize(pointsNum);
for (int i = 0; i < pointsNum; ++i)
{
reader.ReadNextPoint();
const liblas::Point& p = reader.GetPoint();
float px = (float)p.GetX();
float py = (float)p.GetY();
float pz = (float)p.GetZ();
float pi = (float)p.GetIntensity();
uint16_t pr = p.GetColor().GetRed();
uint16_t pg = p.GetColor().GetGreen();
uint16_t pb = p.GetColor().GetBlue();
int r2 = ceil(((float)pr / 65536) * (float)256);
int g2 = ceil(((float)pg / 65536) * (float)256);
int b2 = ceil(((float)pb / 65536) * (float)256);
uint32_t prgb = ((int)r2) << 16 | ((int)g2) << 8 | ((int)b2);
cloudrgb_ptr->points[i].rgb = *reinterpret_cast<float*>(&prgb);
(*cloudrgb_ptr)[i].x = px;
(*cloudrgb_ptr)[i].y = py;
(*cloudrgb_ptr)[i].z = pz;
}
ifs.close();
}
viewer->removeAllPointClouds();
viewer->removeAllShapes();
viewer->addPointCloud<pcl::PointXYZRGB>(cloudrgb_ptr,"point cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
viewer->resetCamera();
ui.qvtkwidget->update();
}
At first I thought the string was not properly initialized so I tried to change the second parameters of addpointcloud such as
const std::string ptcd{ "point cloud" }; const char* arr = "point cloud";
but there is no effect.

Plot 2D histogram of 4 columns data file by using ROOT

I need to plot 2D histograms of four columns of datasets in a data file.
here is my code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "TFile.h"
#include "TGraphErrors.h"
#include "TH2.h"
using namespace std;
int main() {
// reading an ascii file
int row = 0;
double my_array[1839][4];
ifstream myfile ("test.txt");
if ( myfile.is_open() ) {
string line;
while( getline(myfile,line)) {
cout << line << endl;
stringstream ss(line);
int col=0;
while (ss >> my_array[row][col]) col++;
row++;
}
}
myfile.close();
for (int i=0; i <1839; ++i) {
cout << "my_array " << my_array[i][0] << ", " << my_array[i][1] << "," << m\
y_array[i][2] << my_array[i][3] << endl;
}
TH2D *hitm = new TH2D("hitm","hitmap", 256,-0.5,255.5,256,-0.5,255.5);
for (int i=0; i <1839; ++i) {
double x = my_array[i][0];
double y = my_array[i][1];
double z = my_array[i][2];
double d = my_array[i][3];
return 0;
}
'
it doesn't work yet, all of what it do is printing the columns on the terminal, can you please help me on that ?
thanks in advance

C code translated to Qt crashes while executing

I´m a beginner on programming and Qt, but as liked the framework I´m trying to improve my skills and write my C++ codes on it. I got a task of writing a Ricker wavelet code and then plot it.
I divided it in two tasks, first make the ricker code works, and when it is running, then implement a way to plot it, I will use qcustomplot for it.
I got a code from C and I´m trying to adapt it to Qt. Although it doesn´t give any errors during compilation, when executing it crashes, with the following message:
Invalid parameter passed to C runtime function. C:/Users/Flavio/Documents/qtTest/build-ricker2-Desktop_Qt_5_11_0_MinGW_32bit-Debug/debug/ricker2.exe
exited with code 255
The code I´m supposed to translate is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float *rickerwavelet(float fpeak, float dt, int *nwricker);
int main(int argc, char **argv)
{
int i;
float dt;
float fpeak;
float *wricker=NULL;
int nwricker;
fpeak = atof(argv[1]);
dt = atof(argv[2]);
wricker = rickerwavelet(fpeak, dt, &nwricker);
/* show value of ricker wavelets */
for (i=0; i<nwricker; i++)
printf("%i. %3.5f \n", i, wricker[i]);
free(wricker);
return(1);
}
/* ricker wavelet function, return an array ricker wavelets */
float *rickerwavelet(float fpeak, float dt, int *nwricker)
{
int i, k;
int nw;
int nc;
float pi;
float nw1, alpha, beta;
float *wricker=NULL;
pi = 3.141592653589793;
nw1 = 2.2/fpeak/dt;
nw = 2*floor(nw1/2)+1;
nc = floor(nw/2);
wricker = (float*) calloc (nw, sizeof(float));
for (i=0; i<nw; i++)
{
k = i+1;
alpha = (nc-k+1)fpeakdtpi;
beta = pow(alpha, 2.0);
wricker[i] = (1 - (beta2)) * exp(-beta);
}
(*nwricker) = nw;
return(wricker);
}
The code i wrote on Qt is:
#include <QCoreApplication>
#include <qmath.h>
#include <stdio.h>
#include <stdlib.h>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int i,k,nw,nc;
double *wricker=NULL;
int nwricker = 60;
int wavelet_freq = 30;
int polarity=1;
int sampling_rate=0.004;
float nw1, alpha, beta;
const double pi = 3.141592653589793238460;
nw1 = 2.2/wavelet_freq/sampling_rate;
nw = 2*floor(nw1/2)+1;
nc = floor(nw/2);
wricker = (double*)calloc (nw, sizeof(double));
for (i=0; i<nw; i++)
{
k = i+1;
alpha = (nc-k+1)wavelet_freqsampling_ratepi;
beta = pow(alpha, 2.0);
wricker[i] = polarity((1 - (beta2)) * exp(-beta));
};
/* show value of ricker wavelets */
for (i=0; i<nwricker; i++)
{
qDebug()<<i<<wricker[i];
};
free(wricker);
return a.exec();
}
Analytic expression
The amplitude A of the Ricker wavelet with peak frequency f at time t is computed like so:
A = (1-2 pi^2 f^2* t^2) e^{-pi^2* f^2* t^2}
A py code for it would be:
import numpy as np
import matplotlib.pyplot as plt
def ricker(f, length=0.128, dt=0.001):
t = np.arange(-length/2, (length-dt)/2, dt)
y = (1.0 - 2.0*(np.pi2)(f2)(t2)) * np.exp(-(np.pi2)(f2)(t**2))
return t, y
f = 25 # A low wavelength of 25 Hz
t, w = ricker(f)
What seems quite simple.
Does anyone have any idea what is wrong in my code???
Doing a bit of Debugging I found the problem is when passing the vectors to qDebug, it give a message:
THE INFERIOR STOPPED BECAUSE IT RECEIVED A SIGNAL FROM THE OPERATING
SYSTEM . SIGNAL NAME: SIGSEGV SIGNAL MEANING: SEGMENTATION FAULT
I´ll search for more information on this signal meaning. I used qDebug only with the intention of showing the data on a terminal to make sure the calculation was working.
Thanks in advance.
C++ is much more like Python than C. Even though your C code was particularly convoluted, it still isn't as nice a C++ can be.
A complete example that even plots the data can be very, very simple. If that doesn't show the combined power of C++ and Qt, I hardly know what will :)
wavelet-plot-50690312.pro file
QT = charts
SOURCES = main.cpp
main.cpp
// https://github.com/KubaO/stackoverflown/tree/master/questions/wavelet-plot-50690312
#include <QtCharts>
#include <cmath>
const double pi = 3.14159265358979323846;
QVector<QPointF> ricker(double f, double length = 2.0, double dt = 0.001) {
size_t N = (length - dt/2.0)/dt;
QVector<QPointF> w(N);
for (size_t i = 0; i < N; ++i) {
double t = -length/2 + i*dt;
w[i].setX(t);
w[i].setY((1.0 - 2*pi*pi*f*f*t*t) * exp(-pi*pi*f*f*t*t));
}
return w;
}
QLineSeries *rickerSeries(double f) {
auto *series = new QLineSeries;
series->setName(QStringLiteral("Ricker Wavelet for f=%1").arg(f, 2));
series->replace(ricker(f));
return series;
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QChartView view;
view.chart()->addSeries(rickerSeries(1.0));
view.chart()->addSeries(rickerSeries(2.0));
view.chart()->createDefaultAxes();
view.setMinimumSize(800, 600);
view.show();
return app.exec();
}
Of course, this looks nice in C++. How about C? Let's pretend we had some C binding for Qt. Then it might look as follows:
// https://github.com/KubaO/stackoverflown/tree/master/questions/wavelet-plot-50690312/c-binding
#include "qc_binding.h"
#include <math.h>
#include <stddef.h>
#include <stdio.h>
const double pi = 3.14159265358979323846;
CQVector *ricker(double f, double length, double dt) {
scope_enter();
size_t N = (length - dt/2.0)/dt;
CQVector *vector = CQVector_size_(CQ_, CQPointF_type(), N);
CQPointF *const points = CQPointF_(CQVector_data_at(vector, 0));
for (size_t i = 0; i < N; ++i) {
double t = -length/2 + i*dt;
points[i].x = t;
points[i].y = (1.0 - 2*pi*pi*f*f*t*t) * exp(-pi*pi*f*f*t*t);
}
return scope_leave_ptr(vector);
}
CQLineSeries *rickerSeries(double f) {
scope_enter();
CQLineSeries *series = CQLineSeries_(CQ_);
CQLineSeries_setName(series, CQString_asprintf(CQ_, "Ricker Wavelet for f=%.2f", f));
CQLineSeries_replaceVector(series, ricker(f, 2.0, 0.001));
return scope_leave_ptr(series);
}
int main(int argc, char *argv[]) {
scope_enter();
CQApplication *app = CQApplication_(CQ_, &argc, argv);
CQChartView *view = CQChartView_(CQ_);
CQChart *chart = CQChartView_getChart(view);
CQChart_addLineSeries(chart, rickerSeries(1.0));
CQChart_addLineSeries(chart, rickerSeries(2.0));
CQChart_createDefaultAxes(chart);
CQWidget *view_ = CQWidget_cast(view);
CQWidget_setMinimumSize(view_, 800, 600);
CQWidget_show(view_);
return scope_leave_int(CQApplication_exec(app));
}
With a little bit of work, a C binding can be made that is about as easy to use as C++: scopes are tracked, RAII works, destructors get called when needed, values about to be returned are not destructed, etc.
Such a minimum binding, implementing all that's needed to run the code above, is available at https://github.com/KubaO/stackoverflown/tree/master/questions/wavelet-plot-50690312/c-binding.

QWidget in X11 window?

I am trying to put a QWidget in a X11 window using the QWidget::create function but I get 2 windows instead of one:
class QTkMainWindow: public QWidget {
public:
QTkMainWindow(WId win)
:QWidget() {
QWidget::create(win, true, true);
}
};
void createApp(int argc, char **argv) {
Display *d;
Window w;
XEvent e;
int s;
d = XOpenDisplay(NULL);
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
QApplication *qapp = new QApplication(argc, argv);
QTkMainWindow *win = new QTkMainWindow(w);
win->show();
XMapWindow(d, w);
while (1) {
XNextEvent(d, &e);
qapp->processEvents();
if (e.type == KeyPress)
break;
}
}
Can anyone tell me what am I missing?
Thanks!

I want to pass all the arguments of the sprintf into ivlpp_main using argc and argv. code has same effect or not?

This one is Original.
static void build_preprocess_command(int e_flag)
{
ivlpp_main(argc, argv);
snprintf(tmp, sizeof tmp, "%s%civlpp %s%s -F\"%s\" -f\"%s\" -p\"%s\" ",
pbase, sep, verbose_flag?" -v":"",
e_flag?"":" -L", defines_path, source_path,
compiled_defines_path);
}
This is my code
static void build_preprocess_command_and_run(int e_flag)
{
char **myargv;
int argc, arg;
myargc = 7;
myargv = new char(myargc);
arg = 0;
myargv[arg] = new char (strlen(pbase) + strlen("ivlpp") + strlen(sep) + 2);
sprintf(argv[arg], strlen(pbase) + 2,"s%s%ccivlpp", pbase, sep);
myargv[arg++] = new char (strlen(defines_path) + strlen("-F\”%s\”"));
myargv[arg++] = new char (strlen(defines_path) + strlen("-f\”%s\”"));
myargv[arg++] = new char (strlen(defines_path) + strlen("-f\”%s\”"));
myargv[arg++] = new char (strlen("-p\”%s\”"));
myargv[arg++] = new char (strlen("-v"));
myargv[arg++] = new char (strlen("-L"));
if (verbose_flag) {
//argv[arg++] = "-v";
sprintf("-v");
}
if (!e_flag) {
//argv[arg++] = "-L";
sprintf("-L");
}
snprintf( "-F\"%s\"", defines_path);
snprintf( "-f\"%s\"", source_path);
snprintf( "-f\"%s\"", compiled_defines_path);
ivlpp_main(argc, argv);
}
No. First code is wrong. Second doesn't compile. sprintf has more arguments than the single one you're using. my code on your last question works (this is like a duplicate).

Resources