The Qsqlrelationaltablemodel SELECT statement will change the name of the column

Source: Internet
Author: User

Don't explain what Qsqlrelationaltablemodel is. Look at the following code:

        3.1 Create model        model = new Qsqlrelationaltablemodel ();        Model->settable ("tyzb_xmmx");        Model->setfilter (filter);        Model->setsort (Tyzb_xmmx::xh,qt::ascendingorder);        3.2 The Foreign key column displays the problem        model->setrelation (tyzb_xmmx::d wdm_id,qsqlrelation ("DM_DWDM", "DWDM", "DWMC"));        Model->setrelation (Tyzb_xmmx::xmlb_id,qsqlrelation ("dm_xmlb", "XH", "LBMC"));        Model->setrelation (Tyzb_xmmx::zydm_id,qsqlrelation ("DM_ZYDM", "XH", "ZYMC"));        Model->setrelation (Tyzb_xmmx::zjxx_id,qsqlrelation ("Tyzb_zjxx", "XH", "ZJXM"));        Model->setjoinmode (Qsqlrelationaltablemodel::leftjoin); Rows that have a foreign key column blank are also displayed

Some of the rest of the code is omitted, such as setting the column header display, adjusting the Qtableview, and then calling the following code:

    //4 Execute QueryQsqlrecord r = model->record ();    Qstringlist FieldNames;  for(inti =0; i< Model->columncount (); i++) {fieldnames.append (R.fieldname (i)); } qdebug ()<< Fieldnames.join (','); Model-Select(); R= model->record ();    Fieldnames.clear ();  for(inti =0; i< Model->columncount (); i++) {fieldnames.append (R.fieldname (i)); }qdebug ()<< Fieldnames.join (',');

View the output of the next qdebug:

  xh,createdate,bz,isdelete,lastoperate , Bnd,jzsj,xmmc,nd,ndyq,qssj,xmbh,xmdm,xmsqs,yjnryq,yap,zszjyj,zgs,, xmlb_id , zjxx_id , Gjz,zydm_id , ZYPX,HCMC   " "  xh,createdate,bz,isdelete,lastoperate,bnd,jzsj,xmmc,nd,ndyq,qssj,xmbh,xmdm,xmsqs,yjnryq,yap,zszjyj,zgs,DWMC , LBMC , zjxm , Gjz,ZYMC , ZYPX,HCMC   " 

What is found, the column name of the column with the foreign key has been updated by the SELECT statement to the foreign key display column, if you also call the following statement after the SELECT statement to get the column ordinal, it will be an error, in particular, return-1:

  Model->fieldindex ("zydm_id");   Returns-1

As a comparison, it continues to be called on the model that executed the SELECT call:

    R = Model->record (0);     << R.field (Model->fieldindex ("zymc"));     << R.field (Model->fieldindex ("zymc")). Value ();

Returns the following information:

Qsqlfield ("zymc"7650253false   "collection and Recovery Center ")

That is, the display value of the foreign key is returned, not the foreign key value itself.

So, can you get the original zydm_id value directly in the model? There is no good way to find it, only re-create a qsqlquery statement, based on the primary key XH get:

    Qsqlquery GETZYDM (QString ("select zydm_id from tyzb_xmmx where xh=%1"). Arg (R.value ( TYZB_XMMX::XH). toString ()));     if (Getzydm.first ())         << Getzydm.value (0);

The key value of the foreign key is returned: qvariant (Qlonglong, 7),
It's a very discordant and elegant way! Temporarily so, there is no better please passers-by pointing twos!

The Qsqlrelationaltablemodel SELECT statement will change the name of the column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.