Qt, Error (malloc) when adding mulitple widgets with qcustomplots to window - c++

I am trying to make a Widget that contains multiple widgets with each a QCustomplot and some Plot Data (QLabel and QPushbuttons). This widget is open from a mainwindow with parent=0.
I add the subwidgets with a function, but if it is used more than one time it throws an error:
malloc.c:3757: _int_malloc: Assertion `(unsigned long) (size) >=
(unsigned long) (nb)' failed.
So heres the code, that leads to this:
Opening the widget, that should contain the subwidgets:
Zeitdatenfenster = new Plotfenster();
Zeitdatenfenster->getZeitData(ausgewaehlteKanaele, *messdaten, double(Anfangszeiteingabe->value()), double(Endzeiteingabe->value()));
Zeitdatenfenster->show();
Zeitdatenfenster is in the MainWindow class header : Plotfenster* Zeitdatenfenster;
Constructor:
Plotfenster::Plotfenster(QWidget *parent) :
QWidget(parent),
ui(new Ui::Plotfenster)
{
ui->setupUi(this);
counter=0;
}
getZeitData( ...) -function:
void Plotfenster::getZeitData(const std::vector<bool>& Kanalauswahl, const Messdatenverwaltung& messdaten, double Anfangszeit, double Endzeit){
setWindowTitle("Time Data");
int zahler=0;
for(int i = 0; i<Kanalauswahl.size();i++){
if(Kanalauswahl[i]){
Ploteigenschaften* kanalzeit = new Ploteigenschaften();
std::cout << kanalzeit << std::endl;
geplotteteDatenInhalt.push_back(kanalzeit);
geplotteteDatenInhalt[zahler]->setZeitData(messdaten, i,Anfangszeit,Endzeit);
addPlot(geplotteteDatenInhalt[zahler],zahler);
zahler++;
}
}
}
geplotteteDatenInhalt is a std::vector<Ploteigenschaften*>,
addPlot() is the function where the subwidgets are created. Ploteigenschaften is just a class that stores the data that is plottet and functions to change it.(with the two Qvectors added to the qcustomplot)
AddPlot function:
void Plotfenster::addPlot(Ploteigenschaften* plotDaten, int zahler ){
Plot* aktuellerPlot = new Plot(plotDaten, this);
geplottetes.push_back(aktuellerPlot);
ui->plotcontainer->addWidget(geplottetes[zahler],counter);
counter++;
}
Plot is the subwiget that contains the constuctor:
Plot::Plot(Ploteigenschaften* plotDatens, QWidget *parent) :
QWidget(parent),
ui(new Ui::Plot)
{
ui->setupUi(this);
ui->plotGrid->setColumnStretch(1, 3);
ui->plotGrid->setHorizontalSpacing(20);
derPlot = new QCustomPlot(this);
this->plotDaten = plotDatens;
this->Datensetzten();
}
The programm crashes at the QCustomplot constructor.
In Datensetzten the plot is specified:
void Plot::Datensetzten(){
this->setInfoText();
this->setPlot();
this->setSaveButton();
}
void Plot::setPlot(){
derPlot->addGraph();
derPlot->graph(0)->setData(plotDaten->xDaten,plotDaten->yDaten);
derPlot->xAxis->setRange(plotDaten->xUntereGrenze,plotDaten->xObereGrenze);
derPlot->yAxis->setRange(plotDaten->yMinimum-std::abs(plotDaten->yMinimum)*0.01,plotDaten->yMaximum+std::abs(plotDaten->yMaximum)*0.01);
derPlot->xAxis->setLabel(plotDaten->BeschriftungxAchse);
derPlot->yAxis->setLabel(plotDaten->BeschriftungyAchse);
derPlot->xAxis2->setVisible(true);
derPlot->xAxis2->setTickLabels(false);
derPlot->yAxis2->setVisible(true);
derPlot->yAxis2->setTickLabels(false);
ui->plotGrid->addWidget(derPlot,0,1,2,1);
derPlot->show();
}
Sorry for the long post, but I have no idea where the error is located and would be very glad for som ideas.
Thanks!

Related

QLabel not showing images using QToolButton

I have a small minimal example of a user interface for visualizing images (both .tif, .tiff, .jpg etc) composed of:
1) N.1 QLabel (used to show the image)
2) N.1 Pushbutton (used to upload a folder)
3) N.1 QLineEdit (used to visualize the path)
4) N.2 QToolbuttons (used as left and right to look through images)
I am trying to look through images using the left and the right QToolbuttons but something is not correct and I am not able to see any image. I was looking at this source as an example in order to develop my own implementation and use it for other projects I am developing.
mainwindow.h
private slots:
void on_imageCroppedABtn_clicked();
void on_leftArrowCroppedA_clicked();
void on_rightArrowCroppedA_clicked();
private:
Ui::MainWindow *ui;
QString camADir;
QString fileCamA;
int croppedIndexA;
QStringList croppedFilenamesA;
QDir croppedA;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
croppedIndexA = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_imageCroppedABtn_clicked()
{
QString cdir = QFileDialog::getExistingDirectory(this, tr("Choose an image directory to load"),
fileCamA, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if((cdir.isEmpty()) || (cdir.isNull()))
return;
croppedA.setPath(cdir);
croppedFilenamesA = croppedA.entryList(QStringList() << "*.tiff" << "*.TIFF" << "*.tif" << "*.TIF", QDir::Files);
croppedIndexA = 0;
ui->lineEditfolderA->setText(croppedA.path());
}
void MainWindow::on_leftArrowCroppedA_clicked()
{
croppedIndexA--;
if(croppedIndexA < 0)
croppedIndexA = croppedFilenamesA.size()-1;
if(croppedFilenamesA.size() > 0)
{
ui->labelCroppedA->setScaledContents(true);
ui->labelCroppedA->setPixmap(QPixmap::fromImage(QImage(croppedFilenamesA[croppedIndexA])));
ui->labelCroppedA->show();
}
}
void MainWindow::on_rightArrowCroppedA_clicked()
{
croppedIndexA++;
if(croppedIndexA >= croppedFilenamesA.size())
croppedIndexA = 0;
if(croppedFilenamesA.size() > 0)
{
ui->labelCroppedA->setScaledContents(true);
ui->labelCroppedA->setPixmap(QPixmap::fromImage(QImage(croppedFilenamesA[croppedIndexA])));
ui->labelCroppedA->show();
}
}
I have been trying to change the implementation in many different ways but I always am not able to see images. Can anyone shed a little bit of light on this issue?
QImage ctor requires the full path to an image which is read. You can store a result of calling getExistingDirectory in data member cdir. When you call entryList all files in the passed directory are listed. While creating QImage you need to concatenate dir name with file name from this dir. So you can call:
ui->labelCroppedA->setPixmap(
QPixmap::fromImage(QImage(cdir + "/" + croppedFilenamesA[croppedIndexA])));
^ add directory separator

qt when qtablewidget is sorting,how to add data in same row?

I use QTableWidget to insert data when it is sorting, there has a problem i can't. For example:
QtTest::QtTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
ui.tableWidget->setSortingEnabled(true);
ui.tableWidget->sortByColumn(0, Qt::AscendingOrder);
}
void QtTest::on_pushButton_clicked()
{
static int i = 1;
ui.tableWidget->insertRow(0);
ui.tableWidget->setItem(0, 0, new QTableWidgetItem(tr("a%1").arg(i)));
ui.tableWidget->setItem(0, 1, new QTableWidgetItem(tr("b%1").arg(i+1)));
i++;
}
then click pushbutton, i can't add data to the row i want, Is there a elegant way to solve it?

Qt - Adding data to a TableWidget via another TableWidget

I am stuck on a certain problem with table widgets and I am in need of help. Basically, I am trying to get a table widget on a dialogue screen to get the information inside of it and then fill that information on to another table widget on a main window screen. So, when I click the OK button, it should take the text from one table widget and place it in the other table widget. I tried using the below code, and the build ran. But, as soon as I clicked OK, the program crashed. The table called TableWidgetedit is the table I am trying to copy from and send to the table in the mainwindow, named tablewidget. (just to make so I am not vague, I am trying to copy data from the one table and place it in another table, when a user clicks on the OK button.)
int rows = 6;
int columns = 5;
Ui::MainWindow *mainui;
void EditMode1::on_pushButton_clicked()
{
for (int i = 0; i<columns;++i){
for (int j = 0;j<rows;++j){
QTableWidgetItem *celltxt= TableWidgetedit.item(j,i);
mainui->tableWidget->setItem(j,i,celltxt);
}
}
}
Any and all help is appreciated. Thank you!
(I am a not the best with Qt so if you don't mind explaining what changes you have made and why, that would a great help thank you!).
-UPDATE-
#Jeet here are the code for what im trying to do:
tablemainwindow1.h:
#ifndef TABLEMAINWINDOW1_H
#define TABLEMAINWINDOW1_H
#include <QMainWindow>
#include "tabledialougewindow.h"
namespace Ui {
class TableMainWindow1;
}
class TableMainWindow1 : public QMainWindow
{
Q_OBJECT
public:
explicit TableMainWindow1(QWidget *parent = 0);
~TableMainWindow1();
private slots:
void on_pushButton_clicked();
private:
Ui::TableMainWindow1 *ui;
TableDialougeWindow *tbl2;
};
#endif // TABLEMAINWINDOW1_H
tablemainwindow1.cpp:
#include "tablemainwindow1.h"
#include "ui_tablemainwindow1.h"
TableMainWindow1::TableMainWindow1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TableMainWindow1)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(3);
ui->tableWidget->setColumnCount(3);
}
TableMainWindow1::~TableMainWindow1()
{
delete ui;
}
void TableMainWindow1::on_pushButton_clicked()
{
tbl2 = new TableDialougeWindow(this);
tbl2->show();
}
tabledialougewindow.h:
#ifndef TABLEDIALOUGEWINDOW_H
#define TABLEDIALOUGEWINDOW_H
#include <QDialog>
namespace Ui {
class TableDialougeWindow;
}
class TableDialougeWindow : public QDialog
{
Q_OBJECT
public:
explicit TableDialougeWindow(QWidget *parent = 0);
~TableDialougeWindow();
private slots:
void on_buttonBox_accepted();
private:
Ui::TableDialougeWindow *ui;
};
#endif // TABLEDIALOUGEWINDOW_H
tabledialougewindow.cpp:
#include "tabledialougewindow.h"
#include "ui_tabledialougewindow.h"
#include "tablemainwindow1.h"
#include "ui_tablemainwindow1.h"
int Rows = 3;
int Columns = 3;
Ui::TableMainWindow1 *mainui;
TableDialougeWindow::TableDialougeWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TableDialougeWindow)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(Rows);
ui->tableWidget->setColumnCount(Columns);
}
TableDialougeWindow::~TableDialougeWindow()
{
delete ui;
}
void TableDialougeWindow::on_buttonBox_accepted()
{
for(int i = 0;i<Columns;++i){
for(int j = 0;j<Rows;++j){
QTableWidgetItem *celltxt = ui->tableWidget->item(j,i);
QTableWidgetItem *celltxt2 =new QTableWidgetItem(*celltxt);
mainui->tableWidget->setItem(j,i,celltxt2);
}
}
accept();
}
hope this helps.
The error is we cannot insert an item that is already owned by another QTableWidget. So before make a copy we need to clone the data. we can use copy constructor to achieve this. Code comment will explain in detail. Hope this help.
void MainWindow::on_cmdTransfer_clicked()
{
//Source Table
int rows = ui->tbl1->rowCount();
int columns =ui->tbl1->columnCount();
//Destination Table
ui->tbl2->setColumnCount(columns);
ui->tbl2->setRowCount(rows);
//Copy data form one table to another.
for (int i = 0; i<columns;++i){
for (int j = 0;j<rows;++j){
QTableWidgetItem *celltxt= ui->tbl1->item(j,i);
//Clone the data using copy constructor
QTableWidgetItem *celltxt1=new QTableWidgetItem(*celltxt);
ui->tbl2->setItem(j,i,celltxt1);
}
}
}

How do you change a QGraphicsScene from another function?

My program has a QGraphicsView called SimulatorGV and it has a QGraphicsScene called Simulation. In the main function I have drawn a few rectangles and ellipses. I want to be able to draw on the same scene in another function too.
In essence, how do you add shapes to a scene from another function? (Which is called when a button on the user interface is pressed)
So far I've tried to pass the QGraphicsView to the other function? Below is the main ui function (irrelevant statements cut out):
{
ui->setupUi(this);
Simulation = new QGraphicsScene(this);
ui->SimulatorGV->setScene(Simulation);
Simulation->addRect(0,415,20,50,noPen,greyBrush);
Simulation->addRect(425,230,10,420,noPen,goldBrush);
Simulation->addEllipse(80,90,700,700,greyPen,noBrush);
Simulation->addRect(72,215,90,450,noPen,blackBrush);
}
In the header file I declared this function in the private slots:
void DrawingSimulation(QGraphicsView *SimulationGV);
Should it be like this instead?
void DrawingSimulation(QGraphicsScene *Simulation);
I called the function in another function like this:
DrawingSimulation(ui->SimulatorGV);
Should it be like this instead?
DrawingSimulation(ui->SimulatorGV->Simulation);
or?
DrawingSimulation(ui->Simulation);
This is the function I want to be able to draw on the scene from:
void RASP::DrawingSimulation(QGraphicsView *SimulationGV)
{
for (int i = 0; i < DisplayAlphaParticleNumber; i++)
{
if (ParticleLocation[i*6+3] != 0 || ParticleLocation[i*6+6] != 0)
{
ui->SimulatorGV->setScene(Simulation);
Simulation->addEllipse(ParticleLocation[i*6+3],ParticleLocation[i*6+4],10,10);
}
}
}
SimulatorGV is the name of the QGraphicsView in my ui form. RASP is the name of the project. ParticleLocation[i*6+3] is the x coordinate and [i*6+4] is the y coordinate.
Was I right to pass the QGraphicsView onto the varibale instead of the QGraphicsScene Simulation?
Did I pass it correctly?
In the DrawingSimulation function did I add the ellipse correctly?
Edit:
In essence, how do you add shapes to a scene from another function? (Which is called when a button on the user interface is pressed)
When a button is pressed this function is called:
void RASP::on_Button1_clicked()
{
//some values set
//some other functions called then the main one that leads to the drawingsimulation
mainfunction();
}
Then inside the mainfunction():
void RASP::mainfunction()
{
DrawingSimulation(); //is called
}
Now the function DrawingSimulation() which I would like to draw on the original scene in the RASP::RASP() (MyClass::MyClass) is called.
My previous attempt was to have a boolean function that is set true by the button then the addEllipse:
MyClass::MyClass()
{
ui->setupUi(this);
Simulation = new QGraphicsScene(this);
ui->SimulatorGV->setScene(Simulation);
Simulation->addRect(0,415,20,50,noPen,greyBrush);
Simulation->addRect(425,230,10,420,noPen,goldBrush);
Simulation->addEllipse(80,90,700,700,greyPen,noBrush);
Simulation->addRect(72,215,90,450,noPen,blackBrush);
if (SimulationRun == true)
{
for (int i = 0; i < DisplayAlphaParticleNumber; i++)
{
if (ParticleLocation[i*6+3] != 0 || ParticleLocation[i*6+6] != 0)
{
ui->SimulatorGV->setScene(Simulation);
Simulation->addEllipse(ParticleLocation[i*6+3],ParticleLocation[i*6+4],10,10);
}
}
}
}
and then in the button clicked function setting SimulationRun = to true.
Let's keep it simple.
If you have:
MyClass::MyClass() : ui(new Ui::MainWindow)
{
ui->setupUi(this);
Simulation = new QGraphicsScene(this);
ui->SimulatorGV->setScene(Simulation);
Simulation->addRect(0,415,20,50,noPen,greyBrush);
Simulation->addRect(425,230,10,420,noPen,goldBrush);
Simulation->addEllipse(80,90,700,700,greyPen,noBrush);
Simulation->addRect(72,215,90,450,noPen,blackBrush);
}
If this works, then, this will work too:
MyClass::MyClass() : ui(new Ui::MainWindow)
{
ui->setupUi(this);
Simulation = new QGraphicsScene(this); // supposing MyClass has a Simulation attribute of type QGraphicsScene
ui->SimulatorGV->setScene(Simulation);
addItems(); // calling function below
}
void MyClass::addItems()
{
// declared "void addItems();" in header file
addItems( Simulation ); // calling function below that could be static
}
void MyClass::addItems( QGraphicsScene* simu )
{
// declared "static void addItems(QGraphicsScene* simu);" in header file
simu->addRect(0,415,20,50,noPen,greyBrush);
simu->addRect(425,230,10,420,noPen,goldBrush);
simu->addEllipse(80,90,700,700,greyPen,noBrush);
simu->addRect(72,215,90,450,noPen,blackBrush);
}
Then, if this works, you now know how to modify the secene from "another function".
Finally, you should also have:
void MyClass::DrawingSimulation()
{
// declared "void DrawingSimulation();" in header file
DrawingSimulation( Simulation );
}
void MyClass::DrawingSimulation(QGraphicsScene *simu)
{
// declared "void DrawingSimulation(QGraphicsScene *simu);" in header file
for (int i = 0; i < DisplayAlphaParticleNumber; i++)
{
if (ParticleLocation[i*6+3] != 0 || ParticleLocation[i*6+6] != 0)
{
simu->addEllipse(ParticleLocation[i*6+3],ParticleLocation[i*6+4],10,10);
}
}
}
Note that DrawingSimulation() could also be a slot (declare it using public slots: in your header file. Then, if you connect it to the clicked() signal of a QPushButton of your GUI (for instance), it will be called when the button is clicked and ellipse will be added.
Like this:
MyClass::MyClass()
{
...
connect( ui->pushButton, SIGNAL(clicked()), this, SLOT(DrawingSimulation()) );
}

QtableWidget does not show data

I have a class that creates random data which I would like to show in a tableview on the main window.
I added via Designer a table view to the main window and called it tblData.
I suspect the problem is related to this because when I call the constructor the ui file with some implementation is already there.
I have taken the "Detailed Description" section from http://qt-project.org/doc/qt-5/qtablewidget.html as guidance....
However, the table remains empty. I do not see why... Thank you very much.
include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList headers;
headers << "Datapoints";
Dataset *myData;
myData = new Dataset();
myData->createRandomData(10); // create a ten element vector
QVector<int> data;
data = myData->getDataVector(); // data vector created in class Dataset
qDebug() << data;
int i;
for (i = 0 ; i < data.size() ; i++){
QString datapoint;
datapoint = QString::number(data[i]);
QTableWidgetItem * newItem = new QTableWidgetItem(datapoint);
ui->tblData->setItem(i, 0, newItem); // works not
qDebug() << datapoint; // works
}
}
MainWindow::~MainWindow()
{
delete ui;
}
I think you have to define your table's dimensions before starting to populate it with the data, i.e.
ui->tblData->setRowCount(data.size());
ui->tblData->setColumnCount(1);
The reason is that by default the initial row and column count of the table is 0, so the newly added items are not visible.