Results 1 to 5 of 5

Thread: Connect statement problem

  1. #1
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Connect statement problem

    Hi, everybody!
    Please, help me.
    There is a small problem with my code.
    Well, the first part of the code I took from one famous book.
    So, I decided to make the second part myself.
    But it doesn't work...
    The problem is in the connect statement :

    ui->setupUi(this);
    this->setWindowTitle(QString("%1[*] - %2" ).arg("unnamed").arg("SDI"));

    QTextEdit* docWidget = new QTextEdit( this );
    setCentralWidget( docWidget );
    connect( docWidget->document(), SIGNAL(modificationChanged(bool)),
    this, SLOT(setWindowModified(bool)) );

    QAction* cutAction = new QAction( QIcon("C:/icon/icon.png"), tr("Cu&t"), this );
    cutAction->setShortcut( tr("Ctrl+X") );
    cutAction->setStatusTip( tr("Cut") );
    cutAction->setEnabled(false);
    connect( docWidget, SIGNAL(copyAvailable(bool)),
    cutAction, SLOT(setEnabled(bool)) );
    connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()));

    .......

    QAction* pasteAction = new QAction(QIcon("C:/icon/paste.png"), tr("&Paste"), this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("Paste"));
    pasteAction->setEnabled(false);
    Qt Code:
    1. [B]connect(..., SIGNAL(...), pasteAction, SLOT(setEnabled(bool)));[/B]
    To copy to clipboard, switch view to plain text mode 
    connect(pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()));

    I can't understand, which one object (e.g., cutAction, docWidget->document() or this-pointer) should emit which one signal (if exist ) to enable pasteAction?

    Many thanks.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Connect statement problem

    I would think about using the QClipboard::dataChanged() signal to trigger a slot that enables the paste action if the clipboard data is of a suitable type.

  3. The following user says thank you to ChrisW67 for this useful post:

    Bender_Rodriguez (14th April 2013)

  4. #3
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect statement problem

    I would think about using the QClipboard::dataChanged() signal
    Unfortunatley, dataChanged() signal doesn't take any bool parameter. So the signal and setEnabled(bool) aren't compatible

    There is one interesting fact.
    I found exactly the same example project in official reference documentation.
    And you know what did they do to solve this problem?
    *drum-roll*
    Nothing!
    They just ignored this situation (paste function is enabled from the beginning)!
    The point is: no dead body - no case

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Connect statement problem

    I didn't tell you to connect it directly to the action enabled() slot. The clipboard can contain data you cannot paste or be empty, in which case you still do not want the paste action active.

    Something like this during setup:
    Qt Code:
    1. myUpdateClipboardStateSlot(); // sets the initial state
    2. connect(qApp->clipboard(), SIGNAL(dataChanged()), SLOT(myUpdateClipboardStateSlot())); // and follows changes
    To copy to clipboard, switch view to plain text mode 
    and then:
    Qt Code:
    1. void MyObject::myUpdateClipboardStateSlot()
    2. {
    3. const QStringList formats = qApp->clipboard()->mimeData()->formats();
    4. if ( [[a format I can handle is in the list]] ) // or you can call hasText(), hasHtml()...
    5. pasteAction.setEnabled(true);
    6. else
    7. pasteAction.setEnabled(false);
    8. }
    To copy to clipboard, switch view to plain text mode 

    It's also hinted at in the Drag and Drop docs
    Last edited by ChrisW67; 13th April 2013 at 23:44. Reason: spelling corrections

  6. #5
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect statement problem

    The clipboard can contain data you cannot paste or be empty, in which case you still do not want the paste action active.
    You are obviously right.

    Something like this during setup:
    and then:
    Now I get it. Thanks.
    The only way is to wright additional code.
    Keep it simple (c)

    It's also hinted at in the Drag and Drop docs
    Good to know.

Similar Threads

  1. Problem with connect?
    By Jake123 in forum Newbie
    Replies: 13
    Last Post: 23rd January 2013, 16:41
  2. Problem with connect
    By Momergil in forum Newbie
    Replies: 8
    Last Post: 29th July 2011, 01:27
  3. connect() problem
    By harmodrew in forum Newbie
    Replies: 14
    Last Post: 5th August 2010, 17:45
  4. QSqlQuery problem with SELECT statement
    By virtuosite in forum Newbie
    Replies: 2
    Last Post: 31st August 2009, 08:13
  5. Connect Problem
    By nrabara in forum Newbie
    Replies: 3
    Last Post: 4th May 2009, 12:19

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.