QtableWidget does not show data - c++

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.

Related

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?

Filtering files of specific folder using QFileSystemModel and QSortFilterProxyModel subclass

I am trying to filter files of QFileSystemModel using QSortFilterProxyModel. The problem is, I want to only show the contents of a specific folder while filtering. Normally, if I wanted to only show the contents of a specific folder using QFileSystemModel, I would do something like this:
view = new QTreeView(this);
fSystemModel = new QFileSystemModel(this);
view->setModel(fSystemModel);
fSystemModel->setRootPath("C:/Qt");
QModelIndex idx = fSystemModel->index("C:/Qt");
view->setRootIndex(idx);
But when I use the QSortFilterProxyModel, the index has to be the QSortFilterProxyModel's. Since I could not find much information in Qt Documentation regarding this issue, I looked around and found this thread. Using this as a base, I created the following:
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
layout = new QVBoxLayout();
ui->centralWidget->setLayout(layout);
view = new QTreeView(this);
fSystemModel = new QFileSystemModel(this);
filter = new FilterModel();
filter->setSourceModel(fSystemModel);
layout->addWidget(view);
view->setModel(filter);
fSystemModel->setRootPath("C:/Qt");
QModelIndex idx = fSystemModel->index("C:/Qt");
QModelIndex filterIdx = filter->mapFromSource(idx);
qDebug() << filterIdx.isValid();
view->setRootIndex(filterIdx);
}
FilterModel.cpp (QSortFilterProxyModel subclass)
#include "filtermodel.h"
bool FilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
QModelIndex zIndex = sourceModel()->index(source_row, 0, source_parent);
QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
return fileModel->fileName(zIndex).contains("C"); //This line will have custom
//filtering behaviour in the future,
//instead of the name searching one.
}
However, when I run the program, it does not use the specified root index. Moreover, when I use qDebug() to see if the filterIdx is valid, it prints false. What am I doing wrong?
Try to see the result of the next line
qDebug() << idx << " " << fSystemModel->fileName(idx) << " " << filterIdx.isValid();
You can notice that fSystemModel->fileName(idx) is "Qt" (not full path "C:/Qt"). So it doesn't contain "C" from your filter (FilterModel::filterAcceptsRow).

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

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!

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);
}
}
}

Qt - Access a checkbox in a table

I have a table and each row in the table has a checkbox in it's first column. I need to make it so I can detect which checkboxes are checked and delete those rows when a button is pressed.
QWidget * chkWidget = new QWidget();
QHBoxLayout *center = new QHBoxLayout();
center->setAlignment( Qt::AlignCenter );
center->addWidget( new QCheckBox );
chkWidget->setLayout( center );
ui->data_table->setCellWidget(rowCount,0, chkWidget);
Was this done right? If so how do I access the checkboxes at each row?
I talk about a QTableWidget. You can use a QList.You save your QCheckBox into this QList and use it, when there is some change
Maybe you should check out the documentation
QTableWidget: Link
QList: Link
Here is a solution. I cannot run it at the moment, so please tell me if it works. Please validate the row value. I am not sure if it's possible, that row can have the value -1 when you delete the last row ;)
#include "TestTableWidget.h"
#include "ui_TestTableWidget.h"
TestTableWidget::TestTableWidget(QWidget *parent) : QMainWindow(parent), ui(new Ui::TestTableWidget)
{
ui->setupUi(this);
tableWidget = new QTableWidget(this);
tableWidget->setColumnCount(1); // Just an example
ui->gridLayout->addWidget(tableWidget);
connect(tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(slotChange()));
for(int i = 1; i < 10; i++)
{
addRow("Row " + QString::number(i));
}
}
TestTableWidget::~TestTableWidget()
{
delete ui;
}
void TestTableWidget::addRow(QString text)
{
int row = tableWidget->rowCount();
qDebug() << "Current row count is " + QString::number(row);
// Add new one
QTableWidgetItem *item = new QTableWidgetItem(text);
tableWidget->insertRow(row);
tableWidget->setItem(row, 0, item);
// Add item to our list
}
void TestTableWidget::slotChange()
{
int row = tableWidget->currentRow();
qDebug() << "Change in table. Current row-index: " + QString::number(row);
// This value is zero-based, so you can use it in our list
}