Results 1 to 5 of 5

Thread: QDateEdit in QTableWidget

  1. #1
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4

    Default QDateEdit in QTableWidget

    With the following code Put a DateEdit in a Cell of a TableWidget:
    Qt Code:
    1. QDateEdit *dateEdit = new QDateEdit();
    2. dateEdit->setDate(QDate::currentDate());
    3. ui->twHomeworkList->setCellWidget(ui->twHomeworkList->rowCount()-1,1,dateEdit);
    To copy to clipboard, switch view to plain text mode 

    Now I tied to read the Date fromt the Edit:
    Qt Code:
    1. QString x= static_cast<QDateEdit*>(ui->twHomeworkList->cellWidget(1,y))->date().toString();
    To copy to clipboard, switch view to plain text mode 
    But the application just crash.
    Can somebody help me?

    Greets ceddy

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDateEdit in QTableWidget

    You need some more parentheses, something like :
    Qt Code:
    1. QString x = (static_cast<QDateEdit*>(ui->twHomeworkList->cellWidget(1,y)) )->date().toString();
    To copy to clipboard, switch view to plain text mode 
    That is because you need to cast what is returned by cellWidget(...) and then call the date() function from a QDateEdit pointer.

    //you are safer if you use dynamic_cast first, check to be different then null and only then call functions using the pointer, static_cast should be faster, but you need to be absolutely sure that it doesn't fail

    LE: Forget the parenthesis - it should work as you write it... debug your code and make sure that you access and cast the correct coordinates (1, y) that hold the QDateEdit.

    Also dynamic_cast with check for returded null pointer is safer.
    Last edited by Zlatomir; 26th February 2012 at 15:39.

  3. #3
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4

    Default Re: QDateEdit in QTableWidget

    Thanks for the answer, but it doesn't changed anything

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDateEdit in QTableWidget

    Quote Originally Posted by ceddy View Post
    Thanks for the answer, but it doesn't changed anything
    Yes, see the "later edit"

    One issue is that you insert DateEdit on last row, and when you try to access it you always get the "QWidget*" from the first row //and the cast fails

  5. #5
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4

    Default Re: QDateEdit in QTableWidget

    Sorry doesn't mind that
    thanks for your help

    ceddy

Similar Threads

  1. Replies: 2
    Last Post: 21st January 2011, 09:05
  2. Replies: 5
    Last Post: 11th January 2011, 10:52
  3. Null for QDateEdit
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2009, 21:54
  4. QTableWidget + QDateEdit + Delegates
    By Cykus in forum Newbie
    Replies: 1
    Last Post: 8th June 2008, 00:43
  5. Subclassing QDateEdit
    By jamadagni in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2006, 13:26

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
  •  
Qt is a trademark of The Qt Company.