why can't I add features to a exists layer? - qt

I have the shapefile loaded successfully! And init the project instance.
I try add some features to exists layer! But no matter what feature type(point, line, polyline) i try, it just does not work!
hope some friends to help me, I'll be very appreciate it! Thanks in advance!!!
here's my code!
QVector<QgsVectorLayer*> vectorLayers = QgsProject::instance()->layers<QgsVectorLayer*>();
if(vectorLayers.size()<1){
return false;
}
QgsVectorLayer* layer = vectorLayers[0];
auto cap = layer->dataProvider()->capabilities();
qDebug()<< "cap:"<< layer->isValid();
if(cap&QgsVectorDataProvider::AddFeatures){
layer->startEditing();
qDebug()<< "cap:"<< layer->isEditable();
qDebug()<< "support to edit feature";
auto feat = QgsFeature(layer->fields());
feat.setAttribute(0, "hello");
QgsPolyline l;
l << QgsPoint(x,y) << QgsPoint(x+100,y+100); // 添加若干坐标,组成一条折线
QgsGeometry line = QgsGeometry::fromPolyline(l);
feat.setGeometry(line);
qDebug()<<"feat:"<<feat.isValid();
qDebug()<< "result:"<<layer->dataProvider()->addFeature(feat);
layer->commitChanges();
}
layer->updateExtents();
return true;

Related

how can I undo the collapsed edges in a specific region using CGAL

I'm creating an application on QT-creator and using CGAL to read .off file as Linear_cell_complex_for_bgl_combinatorial_map_helper , simplify it using edge_collapse method and re-insert the collapsed edges by undo method
void MainWindow ::simplify()
{
SMS::Edge_collapse_recorder <LCC,My_visitor> recorder(lcc);
State state;
My_visitor mvis(state, lcc);
bool ok;
int n = QInputDialog::getInt(this, "", tr("Number of vertices in each cell:"),lcc.number_of_darts(),0, lcc.number_of_darts(),1, &ok);
if (ok){
n=lcc.number_of_halfedges()/2 - 1;
}
SMS::Count_stop_predicate<LCC> stop(n);
int r = SMS::edge_collapse
(lcc
,stop
,CGAL::parameters::halfedge_index_map(get(CGAL::halfedge_index, lcc))
.vertex_index_map(get(boost::vertex_index, lcc))
.get_cost(SMS::Edge_length_cost<LCC>())
.get_placement(SMS::Midpoint_placement<LCC>()).visitor(recorder.visitor(mvis)));
std::cout << "\nFinished...\n" << r << " edges removed.\n"
<< (lcc.number_of_darts()/2) << " final edges.\n" ;
lcc.display_characteristics(std::cout)<<", is_valid="<<CGAL::is_valid(lcc)<<std::endl;
recorder.undo(mvis);
std::cout<<"re insert "<<std::endl;
lcc.display_characteristics(std::cout)<<", is_valid="<<CGAL::is_valid(lcc)<<std::endl;
}
any Ideas or useful links about :
-re-inserting collapsed edges in specific region only , not all collapsed edges?
I appreciate any help .
The link above is a proof of concept, which enables to undo for a CGAL::Surface_mesh and for OpenMesh but not for a CGAL::Polyhedron_3 or a CGAL::LCC.

pleora sdk convert PvBuffer or PvRawData to QByteArray

I'm using the pleora sdk to capture images from an external camera and I am able to successfully write the data to tiff image files on disk. My next step is to change the data storage to SQLite instead of disk files.
I have PvBuffer *lBuffer pointer working fine. Now I need to convert that data to a format I can use to write to SQLite. I'm using Qt on linux so the QByteArray would be very convenient.
This is kind of a specific question for the pleora sdk and Qt. I'm hoping someone has experience with this.
PvRawData *rawData = lBuffer->GetRawData();
QByteArray ba;
//Need to copy the data from rawData to ba.
Thank you in advance.
I found an answer and wanted to post in case anybody else has something similar. I uses the reintepret_cast method.
data = lBuffer->GetDataPointer()
imgSize = lBuffer->GetPayloadSize();
const char *d = reinterpret_cast<char *>(data);
QByteArray ba(d, imgSize);
QSqlQuery q = QSqlQuery( db );
q.prepare("INSERT INTO imgData (image) values (:imageData)");
q.bindValue(":imageData", ba);
if ( !q.exec() )
qDebug() << "Error inserting image into table: " << q.lastError() << endl;
else
qDebug() << "Query executed properly" << endl;

QGraphicsVideoItem in QGraphicsView

I am trying to draw video inside QGraphicsView in QT5.5. Here's the code
QString path = "video.mp4";
qDebug() << QFile::exists(path);
ui->graphicsView->setScene(new QGraphicsScene());
QMediaPlayer pl;
QGraphicsVideoItem vid;
pl.setVideoOutput(&vid);
qDebug() << pl.error();
vid.setSize( QSizeF(1920, 1080) );
pl.setMedia( QUrl::fromLocalFile( path ) );
qDebug() << pl.error();
ui->graphicsView->scene()->addItem(&vid);
ui->graphicsView->resize(1920, 1080);
pl.play();
qDebug() << pl.error();
Nothing is drawn. No errors. File exists. Maybe some issue with plugins? Or hardware acceleration, cause I am on notebook with discrete card. Thank you in advance.
Ok, I got it. My stupid mistake. QMediaPlayer and QGraphicsVideoItem was deleted after exiting scope.
In case someone else runs into this page. You may have to use a QVideoWidget instead of a QGraphicsVideoItem.
https://whynhow.info/30713/How-to-make-friends-QCamera-and-QGraphicsVideoItem
// Note: QGraphicsVideoItem doesn't work but QVideoWidget does
// https://whynhow.info/30713/How-to-make-friends-QCamera-and-QGraphicsVideoItem?
auto *vidWidget = new QVideoWidget;
mpScene->addWidget(vidWidget);
mpCamera = new QCamera(acCamera, this);
mpCamera->setViewfinder(vidWidget);
mpCamera->setCaptureMode(QCamera::CaptureVideo);

DicomImage's writeBMP function produces unclear gray image

I am using DCMTK to read and hopefully modify DICOM images. I have the following code:
#include <iostream>
#include <opencv\cv.h>
#include <dcmtk\dcmimgle\dcmimage.h>
int main() {
try {
DicomImage* dicomImage = new DicomImage("C:/Users/Kriselle/Documents/000004.dcm");
if ((dicomImage != NULL) && (dicomImage->isMonochrome())) {
dicomImage->writeBMP("C:/Users/Kriselle/Documents/z.bmp", 8);
std::cout << "z.bmp is created" << std::endl;
}
else {
std::cout << "dicomImage is null or not monochrome" << std::endl;
}
}
catch (cv::Exception e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
All I did was create a DicomImage and write its pixel data to a BMP file with the filename I specified but the image returns only a gray image with the outline of the original image barely recognized.
This is what it should look like: https://www.dropbox.com/s/6dw8nlae8hfvqf6/000004.jpg?dl=0
This is what the code produces: https://www.dropbox.com/s/fff2kch124bzjqy/z.bmp?dl=0
Am I missing something in the code or did I not understand what the function does? Can anyone please enlighten me? Thank you very much!
As you can read in the API documentation of the DicomImage class, the default is that no VOI transformation is enabled when rendering monochrome DICOM images. In your case, this seems to be inappropriate, so you should specify a more appropriate VOI setting (e.g. a min-max window) or use one of the VOI windows stored in the DICOM dataset (if any).
By the way, after loading the image in the constructor, you should also check the status of this process using the getStatus() method.

Transparent image in QT

i am new to QT, i got to know how to load a simple image on a window.
i want to know how to make transparent?.
please tell me the way to achieve it.
Thanks
This is how I did it:
canvas = new QImage(":/Zuerich.jpg");
city = new QImage(canvas->size(),QImage::Format_ARGB32);
QPainter p(city);
p.setOpacity(0.1);
p.drawImage(0,0,*canvas);
p.end();
// the proof:
QRgb pix = city->pixel(10,10);
qDebug() << "Alpha" << qAlpha(pix);

Resources