Results 1 to 8 of 8

Thread: Combobox + bool + DataWidgetMapper not good

  1. #1
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Combobox + bool + DataWidgetMapper not good

    Hello,

    64-bit Ubuntu 8.10 (version of Qt and C++ that came with distro)
    8.3.4 PostgreSQL

    Table layout as follows:

    CREATE TABLE expenses (
    -- tran_id serial not null primary key,
    tran_dt date,
    category char(25) CONSTRAINT valid_cat REFERENCES categories (category) MATCH FULL ON DELETE RESTRICT,
    tax_ded boolean,
    payee char(50) CONSTRAINT valid_payee REFERENCES payees (payee) MATCH FULL ON DELETE RESTRICT,
    amount numeric(10,2) CONSTRAINT amt_constraint NOT NULL);



    here are some snippets for the mapping

    tax_dedComboBox = new QComboBox;
    tax_dedComboBox->addItem( "true");
    tax_dedComboBox->addItem( "false");

    tax_dedLabel = new QLabel(tr("Ta&x_Ded:"));
    tax_dedLabel->setBuddy(tax_dedComboBox);



    mapper = new QDataWidgetMapper(this);
    mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    mapper->setModel(tableModel);
    mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    mapper->addMapping(tran_dtDateEdit, tableModel->fieldIndex("tran_dt"));
    mapper->addMapping(categoryComboBox, tableModel->fieldIndex("category"));
    mapper->addMapping(tax_dedComboBox, tableModel->fieldIndex("tax_ded"));
    mapper->addMapping(payeeComboBox, tableModel->fieldIndex("payee"));
    mapper->addMapping(amountLineEdit, tableModel->fieldIndex("amount"));



    The combobox has to have the values "true" and "false" since that is the way the mapper seems to return them. It does not translate values correctly when performing inserts, but will correctly set the combobox value if the boolean exists in the database.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Combobox + bool + DataWidgetMapper not good

    Is there a question here somewhere?

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Combobox + bool + DataWidgetMapper not good

    Just about all the QWidgets you map to are not really able to support NULL, i.e. distinguish between 0 and NULL, "" and NULL, false and NULL...
    So be careful when mapping column that might be NULL to a widget.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Combobox + bool + DataWidgetMapper not good

    It's enough to provide an alternative item delegate that handles such cases. Widgets themselves have nothing to do with it, at least not directly. It is the delegate that sets the data on them and reads it back to the model.

  5. #5
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Combobox + bool + DataWidgetMapper not good

    Quote Originally Posted by caduel View Post
    Just about all the QWidgets you map to are not really able to support NULL, i.e. distinguish between 0 and NULL, "" and NULL, false and NULL...
    So be careful when mapping column that might be NULL to a widget.
    The database always returns 't' or 'f'. Code in Qt is changing it to 'true' and 'false', then trying to send those words back to the database.

    All of this happened because I made the mistake of trying out the QSqlRelationalTableModel convenience class. I'm going to wait for Qt 6 to come out before trying that class again. It appears to only have been tested with SQLite at this point. Two days wasted trying to make a "convenience" class work when the entire little program can be coded the hard way in a day.

    I flagged the item here because the developers haven't been testing with boolean columns. Hopefully, pointing this out and providing a simple table layout will give them additional test cases to work with in the future.

    tax_2138=# \d expenses
    Table "public.expenses"
    Column | Type | Modifiers
    ----------+---------------+------------------------------------------------------------
    tran_id | integer | not null default nextval('expenses_tran_id_seq'::regclass)
    tran_dt | date |
    category | character(25) |
    tax_ded | boolean |
    payee | character(50) |
    amount | numeric(10,2) | not null
    Indexes:
    "expenses_pkey" PRIMARY KEY, btree (tran_id)
    Foreign-key constraints:
    "valid_cat" FOREIGN KEY (category) REFERENCES categories(category) MATCH FULL ON DELETE RESTRICT
    "valid_payee" FOREIGN KEY (payee) REFERENCES payees(payee) MATCH FULL ON DELETE RESTRICT

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Combobox + bool + DataWidgetMapper not good

    Of course you could have simply provided a proper delegate (like at least the relational table delegate or something smarter if you need it)... :-)

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Combobox + bool + DataWidgetMapper not good

    (But how does a proper delegate help with the issue of "how to represent (or set) NULL and how to distinguish it from an empty string" with a QLineEdit? Of course, you could provide an extra checkbox or something like that... is that what you meant?)

    Happily waiting for good ideas (really!)

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Combobox + bool + DataWidgetMapper not good

    This depends on the exact usecase. For instance if you had NULL before and the field is empty, it should probably stay null. If it wasn't empty and now it is, it could become one but it doesn't have to. An automagic delegate won't solve it for you - you have to make the decision and implement it in appropriate methods.

Similar Threads

  1. do we can't compare QIcon or QPixmap????
    By sudheer in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 14:53
  2. Thread Problem
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2006, 17:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.