Labels

08/03/2011

some Qt hints

1)
Pushbotton toggled slot:

To make the pushbotton toggled slot working, two parameters need to setup:

btn2->setCheckable(TRUE);
btn2->setChecked(TRUE);
 
2)
ListView application:

In QListView, i need to display all files in a directory, and pass the file name when i click a file from list.
 
QDirModel *dirModel = new QDirModel;
ui->listView->setModel(dirModel);
ui->listView->setRootIndex(dirModel->index(QString(QApplication::applicationDirPath()).append("/db")));

connect(ui->listView,SIGNAL(clicked(const QModelIndex &)),this,SLOT(selectionChanged(const QModelIndex &)));
 
QString hiscDialog::selectionChanged(const QModelIndex & index)
{
ui->label_db->setText(index.data().toString());
dbFile = QString(QApplication::applicationDirPath()).append("/db/" + index.data().toString());
return dbFile;
}

3)
QSqlTableModel:
 
After i define a model, and try to read out record from the database, I can only get 256 records each time. 
It looks like the Qt has some buffer to protect the database.  
To get all records from database, i can 

while (cDmodel->canFetchMore())
cDmodel->fetchMore();

4)
Read array from .ini file:

To define syntax in .ini file, the first field in each entry is the index, the second the name of the value, and the value itself comes after an equal sign. also need the size attribute.

This defines a 6-element array called StageNum, where each element has a value called "cycle1" (test.ini):
[StageNum]
size = 6
0/cycle1 = 7
1/cy
cle1 = 2
2/cycle1 = 5
3/cycle1 = 2
4/cycle1 = 3
5/cycle1 = 0
To read array from .ini file:

QSettings m4Settings("test.ini", QSettings::IniFormat);
int arySize = m4Settings.beginReadArray("StageNum");
for (int vL = 0; vL < arySize; vL ++)
{
m4Settings.setArrayIndex(vL);
 
cycleStageNum[vL] = m4Settings.value("cycle1","0").toInt();

 }
m4Settings.endArray();


both read and write:
#include 
#include
#include

int data[] = { 3, 1, 4, 1, 5, 9 };

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QSettings m4Settings("test.ini", QSettings::IniFormat);

// Write a test file
m4Settings.beginWriteArray("StageNum");
for (unsigned int i = 0; i < sizeof(data)/sizeof(int); ++i) {
m4Settings.setArrayIndex(i);
m4Settings.setValue("cy1", data[i]);
}
m4Settings.endArray();

// read it back
int arySize = m4Settings.beginReadArray("StageNum");
qDebug() << "arySize =" << arySize;
for (int vL = 0; vL < arySize; vL ++)
{
m4Settings.setArrayIndex(vL);
qDebug()
<< "index =" << vL
<< "cy1 =" << m4Settings.value("cy1", "0").toInt();
}
m4Settings.endArray();

return app.exec();
}

5) 
Increment label id in loop For()

in my program, i have 10 labels, which indicate 10 led lights.
I can change the color for each label by using

ui->label_led_1->setPixmap(picLedRed);
ui->label_led_2->setPixmap(picLedRed);
...

To simplify this code by using a loop:

for (int i = 1; i <= 10; i++)
{
QLabel *label = qFindChild(this, "label_led_" + QString::number(i));
if (label) 
      label->setPixmap(picLedRed); 
}



No comments:

Post a Comment