PDA

View Full Version : Displaying data column into combobox



deck99
7th March 2011, 01:30
Hi........
I'm new to Qt. in my application, i have a file which consist of columns with values and i want to show first column values into combobox list and also take duplicates out. and in second column of values i want to show minimum and maximum value in that column in a label. how to do it? my columns look like this:

Station Ammonia
1 6
1 4
1 7
1 23
2 5
2
2
2
2
3
3
3
3
4
4
4

qlands
7th March 2011, 08:50
Hi,

If your file is a text file you can use:


QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}


process_line is your process that moves a item of the file into the combo taking ignoring the duplicates.

If you need more than one column in the comboBox. You can use a QTableWidget to store the values and then set the QTableWidget to the combo by using setView ( QAbstractItemView * itemView ). The documentation on QTableWidget shows you how to add items to a QTableWidget.

You can see this thread for more information http://www.qtforum.org/article/33282/qcombobox-with-two-columns-solved.html

Carlos.

deck99
7th March 2011, 13:27
Hi...... nw my prroblem is
i my application i am using comboBox to display a list.

now the problem is that when i click the combo box then the no of items in the combo box list insted of displaying in a scroll window are displayed all at a time and since i am having a big list, the display covers most part of my screen........

'Station' column is too large
but i want minimun and maximum value of column 'Ammonia '

How to solve this problem???????????

qlands
7th March 2011, 13:51
Hi,

Can you post an screen shot?

Check setMaxVisibleItems()

Carlos.

deck99
7th March 2011, 15:43
Can u tell me how to read minimum and maximum values from column Ammonia and show it on label?

How to slove this problem????????

stampede
7th March 2011, 15:55
Read the file line-by-line, then for each line : split the line into two parts separated by white space - now if you get two values, update your current max and min values according to second value from split.
Some useful methods:
QString::split (http://doc.qt.nokia.com/latest/qstring.html#split)
QString::toInt (http://doc.qt.nokia.com/latest/qstring.html#toInt)
Then convert values to string : QString::number (http://doc.qt.nokia.com/latest/qstring.html#number-4)
or use this one
QString::arg (http://doc.qt.nokia.com/latest/qstring.html#arg-10)
and set text on label:
QLabel::setText (http://doc.qt.nokia.com/latest/qlabel.html#text-prop)
If you'll have any problems with implementation we'll be happy to help you (at least I will:)), but first you need to try yourself.

deck99
8th March 2011, 00:55
i'm sending my full code and attached UI of the code which i have done. help me out. b123.txt is my column data file and jgj.txt is what i select in the UI.


#include "profile.h"
#include "ui_profile.h"
#include<QTextStream>
#include<QFile>

QString a1[50];

int line3=1;
int l3,j1;
QStringList r;
QStringList dep;

Profile::Profile(QWidget *parent) :
QDialog(parent),
ui(new Ui::Profile)
{
ui->setupUi(this);
ui->comboBox->clear();
QFile file("b123.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString s;
while(!in.atEnd())
{s=in.readLine();
if(line3==1)
{

QStringList l=s.split("\t");
l3=l.size();
for(int i=0;i<l3;i++)
{


QString q=l.at(i);
a1[i]=q;
// out1<<a[i]<<"\t";
}


}

line3++;
}

in.seek(0);
line3=1;
for(j1=0;j1<l3;j1++)
{
if(a1[j1]=="Station")
{
break;
}
}
while(!in.atEnd())
{s=in.readLine();
if(line3!=1)
{

QStringList l=s.split("\t");
l3=l.size();

QString q=l.at(j1);
r.append(q);

}

line3++;
}

in.seek(0);
line3=1;
//min Depth
for(j1=0;j1<l3;j1++)
{
if(a1[j1]=="Depth")
{
break;
}
}
while(!in.atEnd())
{s=in.readLine();
if(line3!=1)
{

QStringList l=s.split("\t");
l3=l.size();

QString q=l.at(j1);
dep.append(q);
// out1<<a[i]<<"\t";


}

line3++;
}
dep.removeDuplicates();
dep.sort();


ui->lineEdit->setText(dep.takeFirst());

file.close();

r.removeDuplicates();
r.sort();
for(int k=0;k<r.size();k++)
{
ui->comboBox->addItem(r[k]);
}

for(j1=0;j1<l3;j1++)
{
if(a1[j1]=="Depth")
{
break;
}
}


for(int k=j1;k<l3;k++)
{

ui->comboBox_3->addItem(a1[k]);

}

}

stampede
8th March 2011, 07:45
First of all, please modify your post and add [CODE] tags, because the code you've posted is hard to read.
What exactly is your problem now ? I think you want to do too many things at once. Start by creating a method that will parse your file and extract min and max values from second column. Don't worry about ui for now, just use qDebug() to test it. Then you can think about displaying the result in ui ( when you'll know that its correct ).

deck99
8th March 2011, 10:45
i'm reading from b123.txt file
first i want to make a file selected station with corresponding depth values and selected parameters from UI.
Secondly, i want to show from that file min and max of depth values on ui. help me to solve the problem
regards
deck99



#include "profile.h"
#include "ui_profile.h"
#include<QTextStream>
#include<QFile>

QString a1[50];

int line3=1;
int l3,j1;
QStringList r;
QStringList dep;

Profile::Profile(QWidget *parent) :
QDialog(parent),
ui(new Ui::Profile)
{
ui->setupUi(this);
ui->comboBox->clear();
QFile file("b123.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString s;
while(!in.atEnd())
{s=in.readLine();
if(line3==1)
{

QStringList l=s.split("\t");
l3=l.size();
for(int i=0;i<l3;i++)
{


QString q=l.at(i);
a1[i]=q;
// out1<<a[i]<<"\t";
}


}

line3++;
}

in.seek(0);
line3=1;
for(j1=0;j1<l3;j1++)
{
if(a1[j1]=="Station")
{
break;
}
}
while(!in.atEnd())
{s=in.readLine();
if(line3!=1)
{

QStringList l=s.split("\t");
l3=l.size();

QString q=l.at(j1);
r.append(q);

}

line3++;
}

in.seek(0);
line3=1;
//min Depth
for(j1=0;j1<l3;j1++)
{
if(a1[j1]=="Depth")
{
break;
}
}
while(!in.atEnd())
{s=in.readLine();
if(line3!=1)
{

QStringList l=s.split("\t");
l3=l.size();

QString q=l.at(j1);
dep.append(q);
// out1<<a[i]<<"\t";


}

line3++;
}
dep.removeDuplicates();
dep.sort();


ui->lineEdit->setText(dep.takeFirst());

file.close();

r.removeDuplicates();
r.sort();
for(int k=0;k<r.size();k++)
{
ui->comboBox->addItem(r[k]);
}

for(j1=0;j1<l3;j1++)
{
if(a1[j1]=="Depth")
{
break;
}
}


for(int k=j1;k<l3;k++)
{

ui->comboBox_3->addItem(a1[k]);

}

}

stampede
9th March 2011, 08:44
After looking at your code I don't know what it does, it's very enigmatic - such variable names a1, l3, j1, r doesn't really help others to understand what you mean.
Provide more detailed explanation of your current algorithm, because now I don't know how could I help you.