从QT复选框到Postgresql的星期选择 [英] Day(s) of week selection from QT checkbox to Postgresql

查看:126
本文介绍了从QT复选框到Postgresql的星期选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组复选框代表我的Qt应用程序GUI中的一周中的几天,我选择一天或多天,根据哪些框被选中,传递一个查询字符串到PostgreSQL为了显示某些数据天 - 例如果我检查星期一和星期三,提取(dow从时间戳)= 1或提取(dow从时间戳)= 3 应该添加到我的查询。我刚刚打了一个粗糙的解决方案 - 虽然还没有测试,因为我写这个,但我想知道是否有一个更短,更优雅的方法,我错过了这里。代码如下: queryAdditionCalltimePart queryAdditionCallStampPart 字符串后来添加到我的主查询的 QString 之前执行主查询 -

I have a group of checkboxes representing the days of a week in my Qt application GUI and I select one or many days, and depending on which boxes are checked, pass a query string to PostgreSQL in order to display certain data on those days -e.g. if I checked monday and wednesday, extract (dow from timestamp) = 1 or extract(dow from timestamp) = 3 should be added to my query. I have just typed a crude solution -though haven't tested yet as I write this-, but I was wondering if there is a shorter -and more elegant- approach that I'm missing out here. The code is as below: -the queryAdditionCalltimePart and queryAdditionCallStampPart strings are later added to the relevant parts of my main query's QString before the main query is executed-

bool checkboxArray[7];
bool mult = false;
checkboxArray[0] = this->ui->checkBoxMonday->isChecked();
checkboxArray[1] = this->ui->checkBoxTuesday->isChecked();
checkboxArray[2] = this->ui->checkBoxWednesday->isChecked();
checkboxArray[3] = this->ui->checkBoxThursday->isChecked();
checkboxArray[4] = this->ui->checkBoxFriday->isChecked();
checkboxArray[5] = this->ui->checkBoxSaturday->isChecked();
checkboxArray[6] = this->ui->checkBoxSunday->isChecked();
QString queryAdditionCalltimePart = "";
QString queryAdditionCalStampPart = "";
int count = 0;

queryAdditionCalltimePart.append("(");
queryAdditionCalStampPart.append("(");
for(int i = 0; i < 7; i++)
{
    if(checkboxArray[i] == true)
    {
        count++;
    }
}


int x = 0;

for(int i = 0; i < 7; i++)
{
    if(checkboxArray[i] == true)
    {
        queryAdditionCalltimePart.append("(SELECT EXTRACT(DOW FROM calltime) = '" +QString::number(i+1)+"')");
        queryAdditionCalStampPart.append("(SELECT EXTRACT(DOW FROM cal.stamp) = '" +QString::number(i+1)+"')");

    }

        if(count > 1 && checkboxArray[i] == true)
    {
        if(x == count - 1)
        {
        }
        else
        {
            queryAdditionCalltimePart.append("OR");
            queryAdditionCalStampPart.append("OR"); 
            x++;
        }
    }
}


queryAdditionCalltimePart.append(")");
queryAdditionCalStampPart.append(")");


推荐答案

您可以向Qt中的任何窗口小部件添加属性, a href =http://qt-project.org/doc/qt-4.8/qobject.html#setProperty =nofollow> http://qt-project.org/doc/qt-4.8/qobject。 html#setProperty 。该属性可以有您想要的任何信息。

You can add properties to any widget in Qt, http://qt-project.org/doc/qt-4.8/qobject.html#setProperty. The property can have any information that you want.

在特定情况下,将SQL字符串作为每个复选框的属性添加更为清晰。

In your particular case, it would be cleaner to attach the SQL string as a property for each checkbox.

this->ui->checkBoxMonday->setProperty("sql", 
        "(SELECT EXTRACT(DOW FROM calltime) = '" +QString::number(i+1)+"') OR ";


$ b b

收到用户输入后,只需添加复选框属性,然后删除最终的OR。

Once you receive the user input, simply append the check box properties and remove the final OR.

这篇关于从QT复选框到Postgresql的星期选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆