I use "VXYModelMapper" and "QStandardItemModel" in plot chart. I use blow codes but append model to VXYModelMapper is very slow.
VXYModelMapper {
model: myChartClass.newMyChartModel
series: lineSeries
xColumn: 0
yColumn: 1
}
void MyChartClass::setMyChartModel(QStandardItemModel *model)
{
newMyChartModel= model;
emit myChartModelChanged(model);
}
QStandardItemModel* lineModel=new QStandardItemModel(npcArray.size(), 2);
foreach(const QJsonValue & val, npcArray){
double xVal=val.toObject().value("x").toDouble();
double yVal=val.toObject().value("y").toDouble();
QStandardItem *item1 = new QStandardItem(QString::number(xVal));
lineModel->setItem(i, 0, item1);
QStandardItem *item2 = new QStandardItem(QString::number(yVal));
lineModel->setItem(i, 1, item2);
i+=1;
}
//To this line everything is very good and fast
QElapsedTimer timer;
timer.start();
myChartClass->setMyChartModel(lineModel);
qDebug() << "The slow operation took" << timer.elapsed() << "millisecond";
Output: The slow operation took 123045 millisecond. Total point
count: 5188
It is very slow :(
Update:
I use QTimer but not benefit. :(
lineModel=new QStandardItemModel(0, 2);
myChartClass->setMyChartModel(lineModel);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(30);
void MyChartClass::update(){
int toIndex=fromIndex+100;
if(toIndex>=npcArray.size()){
toIndex=npcArray.size()-1;
}
for(int i=fromIndex;i<toIndex;i++){
double xVal=npcArray[i].toObject().value("x").toDouble();
double yVal=npcArray[i].toObject().value("y").toDouble();
QStandardItem *item1 = new QStandardItem(QString::number(xVal));
lineModel->setItem(i, 0, item1);
QStandardItem *item2 = new QStandardItem(QString::number(yVal));
lineModel->setItem(i, 1, item2);
}
fromIndex=toIndex;
}
Related
I'm experimenting with QThreadPool and realized that my program exited with SIGSEGV after QThreadPool reaches expiry timeout.
I started the program creating pointers to QLabel and QLineEdits as placeholders, which are kept in QLists.
Then, the method carregar2() is called, when the menu is clicked, to start the QThreadPool.
I implemented 4 QRunnables. They're all the same: they invoke external program (QProcess) with different parameters. Their result are written to QLineEdits.
If I set setExpiryTimeout(-1), the program does not crash.
This is the MainWindow class, it is the called from the main.cpp :
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QtWidgets>
#include "ClickableLabel.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QMainWindow::showMaximized();
ui->setupUi(this);
QStringList args = QApplication::arguments();
QString path;
if(args.size()<=1)
path = QFileDialog::getExistingDirectory(parent, "", "/studio/FOTOS", QFileDialog::ShowDirsOnly);
else
path = static_cast<QString>(args.at(1));
QDir *dir = new QDir(path);
setWindowTitle("QExif - " + path);
QScrollArea *scroll = new QScrollArea();
QGridLayout *grid = new QGridLayout(scroll);
QFrame *frame = new QFrame();
int row=0;
int col=0;
QStringList filtro;
filtro << "*.jpg";
const QFileInfoList fil = dir->entryInfoList(filtro,QDir::Files );
qDebug() << "FOTOS: " << fil.size();
foreach (QFileInfo fi, fil ) {
QString f = fi.absoluteFilePath();
QLabel *l = new ClickableLabel(); //A custom QLabel that implements a clicked() signal and "emit clicked()" on mousePressEvent
l->setStyleSheet("border: 5px solid white");
l->setMaximumSize(w,h);
l->setMinimumSize(w,h);
l->setProperty("foto", f);
l->setToolTip(f);
connect(l, SIGNAL(clicked()), this, SLOT(abrirVisualizadorExterno()));
grid->addWidget(l,row,col,1,1,Qt::AlignTop);
//tag buttons
QHBoxLayout *box = new QHBoxLayout(parent);
QFrame *btnFrame = new QFrame();
btnFrame->setLayout(box);
btnFrame->setMaximumWidth(w);
QLineEdit *tagArtista = new QLineEdit(parent);
tagArtista->setToolTip("Etiquetas");
grid->addWidget(tagArtista,row+1,col,1,1,Qt::AlignTop);
QLineEdit *tagDescricao = new QLineEdit(parent);
tagDescricao->setToolTip("Descrição da imagem");
grid->addWidget(tagDescricao,row+2,col,1,1,Qt::AlignTop);
QLineEdit *tagDataHora = new QLineEdit(parent);
tagDataHora->setToolTip("Data e Hora");
grid->addWidget(tagDataHora,row+3,col,1,1,Qt::AlignTop);
tagArtista->setProperty("foto", f);
tagDescricao->setProperty("foto", f);
tagDataHora->setProperty("foto", f);
fList->append(f);
labelList->append(l);
tagList->append(tagArtista);
descricaoList->append(tagDescricao);
dataHoraList->append(tagDataHora);
col++;
if(col >3) { col=0; row+=4; }
} //foreach
frame->setLayout(grid);
scroll->setWidget(frame);
setCentralWidget(scroll);
/*
* MENU
* */
QMenu *menuExif = ui->menuBar->addMenu("Exif");
menuExif->addAction("Carregar");
connect(menuExif,SIGNAL(triggered(QAction*)),this,SLOT(menuHandler(QAction*)));
}
void MainWindow::menuHandler(QAction *action){
if(action->text() == "Carregar"){
carregar2();
}
}
void MainWindow::carregar2(){
QThreadPool *pool = QThreadPool::globalInstance();
pool->setExpiryTimeout(-1);
for(int i=0; i<fList->count(); i++){
ImagemRunnable *imageRunnable = new ImagemRunnable(labelList->at(i), fList->at(i), w, h);
QThreadPool::globalInstance()->start(imageRunnable);
TagRunnable *tagRunnable = new TagRunnable(tagList->at(i), fList->at(i));
pool->start(tagRunnable);
TagDescricaoRunnable *tagDescricaoRunnable = new TagDescricaoRunnable(descricaoList->at(i), fList->at(i));;
pool->start(tagDescricaoRunnable);
TagDataHoraRunnable *tagDataHoraRunnable = new TagDataHoraRunnable(dataHoraList->at(i), fList->at(i));
pool->start(tagDataHoraRunnable);
}
}
void MainWindow::abrirVisualizadorExterno(){
ClickableLabel *l = (ClickableLabel *) sender();
qDebug() << "Abrir" << l->property("foto");
if (l->property("foto").isValid()){
QStringList cmd;
cmd << QString("eog %1").arg(l->property("foto").toString());
system(cmd.at(0).toUtf8().data());
}
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event){
qDebug() << watched->property("foto").toString();
return false;
}
MainWindow::~MainWindow()
{
delete ui;
}
And this is one of the implemented QRunnables:
header:
#ifndef TAGRUNNABLE_H
#define TAGRUNNABLE_H
#include <QRunnable>
#include <QLineEdit>
class TagRunnable : public QRunnable, QObject
{
public:
TagRunnable(QLineEdit * f, QString file);
void run() override;
private:
QLineEdit *field;
QString file;
};
#endif // TAGRUNNABLE_H
cpp:
#include "tagrunnable.h"
#include <QProcess>
#include <QDebug>
TagRunnable::TagRunnable(QLineEdit * f, QString file)
{
field = f;
this->file = file;
}
void TagRunnable::run(){
QProcess *proc = new QProcess();
//pessoas na foto
proc->start("/usr/bin/exif", QStringList()
<< "-t" << "0x013b"
<< "-m"
<< file
);
if(!proc->waitForFinished()){
qDebug() << "Timeout ao ler exif tag (pessoas)." << file;
}
field->setText(proc->readAllStandardOutput());
field->setProperty("tagOriginal", field->text());
}
My questions:
Suppose I use 30 seconds as expiry timeout. After that time, is my reference to the QProcess removed from memory and causing the QLineEdit text to be lost as well?
Is it ok to set expiry timeout to -1 in any situation or, perhaps, I'm not using it properly because of other architectural error of my program ?
I have searched the answer, and ui->tableWidget->item( row, col )->text(); seems to be the answer.
Now, in my code I have:
ui-> tableWidget->setItem(row-1, 1, new QTableWidgetItem("clicked"));
This works just fine. When the cell is clicked, it displays the "clicked" message. BUT, when i add this below:
QString str;
QTableWidgetItem * itm = ui->tableWidget->item( 3, 1 );
str = itm->text();
The program crashes.
Any idea how could I view the text from the cell?
All the code.Constructor:
{
ui->setupUi(this);
QDate date = QDate::currentDate();
QString dateString = date.toString();
QFont myFont;
QFontMetrics font_meter(myFont);
int line_width=0;
ui->label->setText(dateString);
QFile file(":/input.txt");
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(0,"info",file.errorString());
}
QTextStream in(&file);
QStringList headerLabels;
QStringList rowLabels;
QStringList rowContents;
headerLabels << "Tasks to do" ;
int row_count=0;
while (true)
{
QString line = in.readLine();
if (line.isNull())
{
file.close();
break;
}
else
{
row_count++;
rowLabels.append(QString("%1").arg(row_count));
rowContents.append(line);
if( font_meter.width(line)>line_width)
{
line_width=font_meter.width(line);
}
}
}
ui->tableWidget->setRowCount(row_count);
ui->tableWidget->setVerticalHeaderLabels(rowLabels);
ui->tableWidget->setColumnCount(1);
// ui->tableWidget->setCellWidget();
for(int i=0;i<row_count;i++)
{
ui-> tableWidget->setItem(i-1, 1, new QTableWidgetItem(rowContents.at(i)));
}
}
slot:
void MainWindow::on_tableWidget_cellDoubleClicked(int row, int column)
{
ui->pushButton->setText( QString("%1").arg(row));
ui-> tableWidget->setItem(row-1, 1, new QTableWidgetItem("clicked"));
QString str;
QTableWidgetItem * itm = ui->tableWidget->item( 3, 1 );
str = itm->text();
}
I have tried to make another program.Just with a simple table and getting the text out of a cell:
ui->setupUi(this);
ui->tableWidget->setItem(1,1,new QTableWidgetItem("lol"));
QString str= ui->tableWidget->item(1,1)->text();
.Exactly the same error
ui->setupUi(this);
ui->tableWidget->setRowCount(1);
ui->tableWidget->setColumnCount(1);
ui->tableWidget->setItem(0, 0, new QTableWidgetItem("Hello World!"));
QTableWidgetItem *temp = ui->tableWidget->item(0, 0);
QString str = temp->text();
Works for me, I only managed to crash the program when rowCount or columntCount were 0.
I need some help on the usage of Qtimer.
I work with Qt 5.0.2 and here my problem :
I am trying to develop a Timer, and the interface is simple :
There is just 2 button : the button "Start", to launch the timer, and the "Pause" Button, and a QtimeEdit to display the time.
This screenshot shows how it looks like : http://img834.imageshack.us/img834/1046/5ks6.png
The problem is that the pause function doesn't work. I have read all the documentation about Qtimer here : http://harmattan-dev.nokia.com/docs/library/html/qt4/qtimer.html and here : qt.developpez.com/doc/5.0-snapshot/qtimer/ , but no result.
This is the source code I have : (I put only what is needed)
// Creation of the Buttons and the time area
void MainWindow::createBottom()
{
bottom = new QWidget();
play = new QPushButton("Launch",this);
pause = new QPushButton("Pause",this);
play->setDisabled(false);
pause->setDisabled(true);
timeEdit = new QTimeEdit(this);
timeEdit->setDisplayFormat("mm:ss");
layout->addWidget(play);
layout->addWidget(pause);
layout->addWidget(timeEdit );
bottom->setLayout(layout);
connect(play, SIGNAL(clicked()), this, SLOT(startSimulation()));
connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
}
// to resume the timer where is was stopped
void MainWindow::resumeSimulation()
{
timer->blockSignals( false );
pause->setText("Pause");
pause->disconnect(SIGNAL(clicked()));
connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
paused = false;
timer->start();
int timeOfPause = time->restart();
int timeTotal = timeOfPause + timeElapsed;
time->addMSecs(-timeTotal);
}
// to Start the timer
void MainWindow::pauseSimulation()
{
timer->blockSignals(true);
pause->setText("Resume");
timer->stop();
play->setDisabled(false);
//pause->setDisabled(true);
pause->disconnect(SIGNAL(clicked()));
connect(pause, SIGNAL(clicked()), this, SLOT(resumeSimulation()));
paused = true;
}
// to Start the timer from zero.
void MainWindow::startSimulation()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this , SLOT(updateTime()));
timer->start(500);
play->setDisabled(true);
pause->setDisabled(false);
}
void MainWindow::updateTime()
{
if(time == NULL)
{
time = new QTime(0,0,0,0);
time->start();
}
//timeEdit->setTime(QTime::fromS(time->elapsed()));
//time = &(time->addMSecs(1000));
if(hasRestart)
{
time->restart();
time->addMSecs(-timeElapsed);
hasRestart = false;
}
else
{
timeElapsed =+ time->elapsed();
}
int seconds = 0;
int minutes = 0;
int hours = 0;
if(!paused)
{
seconds = (timeElapsed/1000)%60;
minutes = (timeElapsed/60000)%60;
hours = (timeElapsed/3600000)%24;
std::cout << "Test : " << hours << ":" << minutes << ":" << seconds << std::endl;
timeEdit->setTime(QTime(0,minutes,seconds,0));
timeEdit->update();
}
}
When I push the Start button, the timer starts well, but when I push "Pause" it only pause it on the graphic interface, but when I resume, it shows the present time as if it hadn't paused.
For instance :
I start.
I pause at 00:05. It blocks apparently the timer.
I wait for 10 seconds. I resume the timer, it shows 00:15 instead of 00:06
How could I fix that ?
Thank you !
EDIT : Thanks Kuba Ober, but could you explain me the code you posted please ?
How does the pause work ?
Below is a SSCCE, tested under both Qt 4.8 and 5.1.
//main.cpp
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QElapsedTimer>
#include <QTime>
class Window : public QWidget {
Q_OBJECT
int m_timerId;
qint64 m_accumulator;
QLabel *m_label;
QElapsedTimer m_timer;
Q_SLOT void on_restart_clicked() {
m_accumulator = 0;
m_timer.restart();
if (m_timerId == -1) m_timerId = startTimer(50);
}
Q_SLOT void on_pause_clicked() {
if (m_timer.isValid()) {
m_accumulator += m_timer.elapsed();
m_timer.invalidate();
} else {
m_timer.restart();
m_timerId = startTimer(50);
}
}
void timerEvent(QTimerEvent * ev) {
if (ev->timerId() != m_timerId) {
QWidget::timerEvent(ev);
return;
}
QTime t(0,0);
t = t.addMSecs(m_accumulator);
if (m_timer.isValid()) {
t = t.addMSecs(m_timer.elapsed());
} else {
killTimer(m_timerId);
m_timerId = -1;
}
m_label->setText(t.toString("h:m:ss.zzz"));
}
public:
explicit Window(QWidget *parent = 0, Qt::WindowFlags f = 0) : QWidget(parent, f), m_timerId(-1) {
QVBoxLayout * l = new QVBoxLayout(this);
QPushButton * restart = new QPushButton("Start");
QPushButton * pause = new QPushButton("Pause/Resume");
restart->setObjectName("restart");
pause->setObjectName("pause");
m_label = new QLabel("--");
l->addWidget(restart);
l->addWidget(pause);
l->addWidget(m_label);
QMetaObject::connectSlotsByName(this);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
#include "main.moc"
QTime totalTime, sinceStart;
void MainWindow::createBottom()
{
bottom = new QWidget();
play = new QPushButton("Launch",this);
pause = new QPushButton("Pause",this);
play->setDisabled(false);
pause->setDisabled(true);
timeEdit = new QTimeEdit(this);
timeEdit->setDisplayFormat("mm:ss");
layout->addWidget(play);
layout->addWidget(pause);
layout->addWidget(timeEdit);
bottom->setLayout(layout);
connect(play, SIGNAL(clicked()), this, SLOT(startSimulation()));
connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
connect(this, SIGNAL(timeChanged(QTime)), timeEdit, SLOT(setTime(QTime)));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this , SLOT(updateTime()));
}
void MainWindow::updateTime() {
emit timeChanged(totalTime.addMSecs(sinceStart.elpased()));
}
void MainWindow::resumeSimulation() {
sinceStart.restart();
timer->start();
}
void MainWindow::pauseSimulation() {
timer->stop();
totalTime = totalTime.addMSecs(sinceStart.restart());
emit timeChanged(totalTime);
}
I made a Timer class for the start, stop, pause and resume
class MyTimer
{
public:
MyTimer();
QTime m_qtime;
int m_accumulator;
void start();
int stop();
void pause();
void resume();
};
MyTimer::MyTimer()
:m_accumulator(0), m_qtime(QTime())
{
}
void MyTimer::start()
{
m_qtime.start();
m_accumulator = 0;
}
int MyTimer::stop()
{
if(!m_qtime.isNull())
{
int l_elapsedTime = m_qtime.elapsed();
m_accumulator += l_elapsedTime;
}
m_qtime = QTime();
return m_accumulator;
}
void MyTimer::pause()
{
if(!m_qtime.isNull())
{
int l_elapsedTime = m_qtime.elapsed();
m_accumulator += l_elapsedTime;
}
}
void MyTimer::resume()
{
if(!m_qtime.isNull())
{
m_qtime.restart();
}
}
I want to pass selected QRadioButton's value from one Window to another. I am confused with the function declaration to accept the text value in Second Window, here is my code.
Window1.cpp
void SelectOS :: processNextButton(){
if(ui->win32->isChecked()){
QString loc = "WIN/32Bit";
SelectSoftware *ss = new SelectSoftware (loc);
this->hide();
ss->show();
}
else
{
//QMessageBox:warning();
}
}
Window2.h
public:
SelectSoftware(const QString &text, QWidget *parent=0);
Window2.cpp
SelectSoftware::SelectSoftware(const QString &text, QWidget *parent):QMainWindow(parent),ui(new ui::SelectSoftware)
{
QString softpath = text;
qDebug << softpath;
}
But when I call
ss = new SelectSoftware();
or
ss= new SelectSoftware(const QString &text, QWidget *parent);
in Window2.cpp, I get the error : no matching function for call to SelectSoftware::SelectSoftware()
Where am I wrong?
UPDATE
Window2.cpp
#include "selectsoftware.h"
#include "ui_selectsoftware.h"
SelectSoftware *ss;
QStringList selectedModuleList;
SelectSoftware::SelectSoftware(const QString &text, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SelectSoftware)
{
ui->setupUi(this);
softpath = text;
setWindowPosition();
getSoftwareDetails();
initializeUi();
}
SelectSoftware::~SelectSoftware()
{
delete ui;
}
void SelectSoftware::setWindowPosition()
{
QDesktopWidget *desktop = QApplication::desktop();
int x = (desktop->width() - size().width())/2;
int y = (desktop->height() - size().height())/2;
move(x, y-50);
setFixedSize(size().width(), size().height());
}
void SelectSoftware::cancel()
{
qApp->exit(0);
}
void SelectSoftware::showMainPage()
{
ss = new SelectSoftware(softpath); // here its creating problem, not going forward and app is crashing!!!
for(int j = 0; j < softwareList.size(); j++){
if(checkBox[j]->isChecked()){
if(!comboBox[j]->currentIndex()){
QMessageBox::warning(this, "Select version !", "Select version number for all selected software");
return;
}
}
}
for(int i = 0; i < softwareList.size(); i++){
if(checkBox[i]->isChecked()){
ss->selectedSoftList.push_back(checkBox[i]->text());
ss->selectedVerList.push_back(comboBox[i]->currentText());
}
}
if(!ss->selectedSoftList.size()){
QMessageBox::warning(this, "No product Selected !", "Select one");
return;
}
else{
SelectionPage* sp = new SelectionPage;
this->hide();
sp->show();
}
}
void SelectSoftware::test(const int id)
{
if(checkBox[id]->isChecked()){
comboBox[id]->setEnabled(true);
comboBox[id]->addItem(" Select anyone ");
QString path = qApp->applicationDirPath() + "/products/" + checkBox[id]->text();
QDir dir;
dir.cd(path);
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
comboBox[id]->addItem(fileInfo.fileName());
}
}else{
comboBox[id]->clear();
comboBox[id]->setDisabled(true);
}
}
void SelectSoftware::getSoftwareDetails()
{
QString fileName = qApp->applicationDirPath() + "/abc/" + SOFTWARELIST;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
QString msg = "Could not find the file " + fileName;
errorExit(msg);
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
processLine(line.toLower());
}
}
void SelectSoftware::processLine(QString str)
{
QStringList list = str.split(",");
QDir path = qApp->applicationDirPath() + "/products/" + list[0];
if(path.exists() && (list.size() == 2)){
QString tmp = list[0];
tmp = tmp.toLower();
softwareList.push_back(tmp);
}
}
void SelectOption::initializeUi()
{
this->setWindowTitle("Window2");
QGridLayout *gridLayout1 = new QGridLayout();
gridLayout1->setMargin(5);
gridLayout1->setSpacing(5);
QSignalMapper* signalMapper = new QSignalMapper();
for(int i = 0; i < list.size(); i++){
radioButton[i] = new QRadioButton();
radioButton[i]->setText(softwareList[i]);
signalMapper->setMapping(radioButton[i], i);
gridLayout1->addWidget(radioButton[i], i/1, i%1);
connect(radioButton[i], SIGNAL(clicked()),signalMapper, SLOT(map()));
}
connect(signalMapper, SIGNAL(mapped(const int &)),this, SIGNAL(radioChecked(const int &)));
connect(this, SIGNAL(radioChecked(const int &)),this, SLOT(test(const int)));
QGridLayout *gridLayout2 = new QGridLayout();
gridLayout2->setMargin(5);
gridLayout2->setSpacing(5);
for(int j = 0; j < list.size(); j++){
comboBox[j] = new QComboBox();
comboBox[j]->setDisabled(true);
gridLayout2->addWidget(comboBox[j], j/1, j%1);
}
QPushButton *nextButton = new QPushButton("Next >");
nextButton->setDefault(true);
connect(nextButton, SIGNAL(clicked()), this, SLOT(showMainPage()));
QPushButton *backButton = new QPushButton("< Back");
backButton->setDefault(true);
connect(backButton, SIGNAL(clicked()), this, SLOT(showSelectOS()));
QPushButton *cancelButton = new QPushButton("Cancel");
cancelButton->setDefault(true);
connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
QHBoxLayout *hboxlayout;
hboxlayout = new QHBoxLayout();
hboxlayout->addLayout(gridLayout1);
hboxlayout->addLayout(gridLayout2);
QHBoxLayout *layout;
layout = new QHBoxLayout();
layout->addStretch(10);
layout->addWidget(nextButton);
layout->addWidget(backButton);
layout->addWidget(cancelButton);
layout->addStretch(10);
QVBoxLayout *mainLayout;
mainLayout = new QVBoxLayout();
mainLayout->addLayout(hboxlayout);
mainLayout->addLayout(layout);
ui->centralwidget->setLayout(mainLayout);
}
QVector<QString> SelectSoftware::getSelectedSoftware()
{
return ss->selectedSoftList;
}
QVector<QString> SelectSoftware::getSelectedVersion()
{
return ss->selectedVerList;
}
QStringList SelectSoftware::getSelectedModules()
{
return selectedModuleList;
}
First of all - use signals and slots, Luke
Second of all, you cannot call ss = new SelectSoftware();, since you haven't declared SelectSoftware constructor without parameters, and calling ss= new SelectSoftware(const QString &text, QWidget *parent); is illegal in C++.
SelectSoftware *ss = new SelectSoftware (loc); is correct, though.
1. In void SelectSoftware::processLine(QString str) addressing to list[0] without checking that list is not empty might be dangerous. I recomend you to add:
if (!list.size())
return;
right after initialization.
2. In void SelectOption::initializeUi() what is list? Are you sure list.size() <= softwareList.size()? If not, it's a potential problem.
3. What is radioButton? I don't see it's initialization. If it is QList < QRadioButton * >, than radioButton[i] = new QRadioButton(); is a bad one and you should do this:
radioButton.append(new QRadioButton());
4. Same goes to comboBox.
Each of the list can cause the crash of your application. And I could easily miss something.
I have a QTableView widget which is filled with the help of QStandardItemModel.
In every two seconds the function containing these two classes is called using QTimer and next 50 entries are given.
The function runs properly after every two seconds but the values arent refreshed in the window displayed.
Thanks in advance for any help.
Code :
void Box::create_frame()
{
k=0;
tablegroup = new QGroupBox(tr("Table"));
QVBoxLayout *layout = new QVBoxLayout;
table = new QTableView(this);
table->setUpdatesEnabled(false);
cout << "recent check" <<endl;
QStandardItemModel *mode = new QStandardItemModel(1,2,this);
mode->setHorizontalHeaderItem(0, new QStandardItem(QString("ID")));
mode->setHorizontalHeaderItem(1, new QStandardItem(QString("DATA")));
map<int,QString>::iterator it;
for(it=dataa.begin();it!=dataa.end();it++)
{
for(int i=0;i<=1;i++)
{
QStandardItem *item;
item = new QStandardItem();
item->setEditable(true);
if(i==0)
{
item->setData(((*it).first), Qt::DisplayRole);
mode->setItem(k,i,item);
}
else
{
item->setData(((*it).second), Qt::DisplayRole);
mode->setItem(k,i,item);
}
}
k++;
}
//setUpdatesEnabled(false);
table->setUpdatesEnabled(true);
cout << "create frame check" << endl;
table->setModel(mode);
layout->addWidget(table);
tablegroup->setLayout(layout);
}
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(refresh()));
timer->start(2000);
}
void dataThread::run()
{
boost::posix_time::seconds delay(2);
int g=0;
int h=50;
while(1)
{
while(g<h)
{
dataa.insert(pair<int,QString>(g+1,"HELLO"));
g++;
}
boost::this_thread::sleep(delay);
h=h+50;
}
}
First, it's late here so maybe I'm overlooking something but from your run loop, it doesn't look like you are actually adding any data to your table's model rather you're just adding entries to your dataa vector/list.
Maybe you should add this to your run loop
while( g < h )
{
dataa.insert(pair<int,QString>(g+1,"HELLO"));
QStandardItem *item;
item = new QStandardItem();
item->setEditable(true);
item->setData( g+1, Qt::DisplayRole );
// You need the model here
table->model()->setItem( h + g, 0, item );
item = new QStandardItem();
item->setData( "Hello", Qt::DisplayRole );
// You need the model again here
table->model->setItem( h + g, 1, item );
++g;
}
Again its late but I think you get the idea. You're currently not changing the model that your table is set to, rather just changing your dataa vector/list.