Results 1 to 9 of 9

Thread: Referencing a Widget with a command

  1. #1
    Join Date
    Mar 2019
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Referencing a Widget with a command

    Hello all and thank you for allowing me the pleasure of asking my noob questions! I am learning by myself so it is nice to know there is a community of people working together! Here is my question.

    Qt Code:
    1. void Edge::on_slotSizeW_textChanged(const QString &arg1)
    2. {
    3. QSettings settings("slots.ini",QSettings::IniFormat);
    4. QString profileName;
    5. QString slotName;
    6.  
    7. QString objName = ui->slotSizeW->objectName();
    8. QString objValue = ui->slotSizeW->text();
    9.  
    10.  
    11. QList<QListWidgetItem*> items = ui->profileSettingsList->selectedItems();
    12. foreach(QListWidgetItem* item, items)
    13. {
    14. profileName.append(item->text());
    15.  
    16. QList<QListWidgetItem*> items = ui->slotList->selectedItems();
    17. foreach(QListWidgetItem* item, items)
    18. {
    19. slotName.append(item->text());
    20.  
    21. settings.beginGroup(profileName);
    22. settings.beginGroup(slotName);
    23. settings.setValue(objName,objValue);
    24. settings.endGroup();
    25.  
    26. qDebug() << objName;
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    in this example is there a way to automatically reference the widget here:

    Qt Code:
    1. QString objName = ui->slotSizeW->objectName();
    2. QString objValue = ui->slotSizeW->text();
    To copy to clipboard, switch view to plain text mode 

    I want to be able to remove ui->slotSizeW and automatically return whichever widget is in focus. Possible? Thank you again

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,430
    Thanked 264 Times in 259 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Referencing a Widget with a command

    I do not really understand what you mean but maybe the solution is QObject::sender ?

  3. #3
    Join Date
    Jul 2012
    Posts
    218
    Thanks
    17
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Referencing a Widget with a command

    Or QApplication::focusWidget() ?

    What do you want to do exactly?

  4. #4
    Join Date
    Mar 2019
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Referencing a Widget with a command

    I am trying to create a function that will automatically write settings to a .ini based off of activity. The current function will set the profile Name and slot name. I have to manually configure each widget for the objName and objValue. I found a function to do it with some widgets but it doesn't work for all. Sorry I am not home or I would post another code example

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,929
    Thanks
    229
    Thanked 615 Times in 605 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Referencing a Widget with a command

    I am not at all sure the code you posted is going to do what you think it will.

    First off, "profileName" and "slotName" are method-level QString instances. So, every time through the loop that starts in line 12, you will add more text to the end of the "profileName" string. It will just grow and grow until you get to the end of the first QList of items.

    Next, "slotName" will also continue to grow, once for every pass through the loop that starts on line 17. But it will continue growing for the next pass though the profile items list, too.

    By the end of both loops, profileName will look like "profile1profile2profile3profile4..." and slotName will look like "slot1slot2slot3slot1slot2slot3slot1slot2slot3slot 1slot2slot3...". I am pretty sure that isn't what you want.

    Next, in the code that writes the settings, you call beginGroup() twice, but only call endGroup() once. They need to be paired up. If you don't pair them, then you INI file will turn into a cascading set of groups because the profileName groups never get closed.

    Next, in every profile name group and every slot name group inside it, you write exactly the same object name and object text pair. If that is really what you want, why bother with the profile name and slot name groups at all? Just write the object name and text to the ini file once, as a top level key / value pair.

    And finally, you use the variable names "item" and "items" for the QList in both the inner and outer loops. The variables in the inner loop "hide" the variables in the outer loop. This is legal C++, but really bad practice. It's confusing, because you have to pay very close attention to where you are when reading the code to know whether "items" and "item" refer to something from the "profileSettingsList" or something completely different from the "slotList". Why don't you name them in a way that makes it easy to keep track of? Like "profileItem" and "profileItems", "slotItem" and "slotItems". Then when you (or someone else) comes along later to edit your code you (or they) will be able to read it and not be confused.

    To answer your original question, if what you are trying to do is connect the same slot to many different widgets (instead of just sizeW), you can do as Lesiok says, use the QObject::sender() method inside the slot. This method returns a pointer to the QObject that emitted the signal that eventually got routed to the slot. You can then use qobject_cast() if you want to cast the QObject pointer into a pointer to the QWidget type that is connected to the slot.

    If these QWidgets are actually QLineEdit, and you are connecting to their QLineEdit::textChanged() signals, then there is no need to retrieve the text using a QLineEdit::text() call. The new text is what is being sent as the QString argument to the slot (the argument you have named "arg1").

    Qt Code:
    1. void Edge::on_slotSizeW_textChanged(const QString &arg1)
    2. {
    3. QSettings settings("slots.ini",QSettings::IniFormat);
    4.  
    5. QObject * pSender = sender();
    6.  
    7. QString objName = pSender->objectName();
    8. QString objValue = arg1;
    9.  
    10. QLineEdit * pLineEdit = qobject_cast< QLineEdit * >( pSender );
    11. // ALWAYS check that the cast worked... because if you don't and it didn't, BOOM!
    12. if ( pLineEdit != nullptr )
    13. {
    14. // ...
    15. }
    16.  
    17. // ...
    18. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Prodeine (7th March 2019)

  7. #6
    Join Date
    Mar 2019
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Referencing a Widget with a command

    And here is my final Answer! Thank you so much for all of your support. I have many more noob questions to come


    Qt Code:
    1. void Edge::on_slotHotkey_keySequenceChanged(const QKeySequence &keySequence)
    2. {
    3. QSettings settings("slots.ini",QSettings::IniFormat);
    4.  
    5.  
    6. QObject * pSender = sender();
    7.  
    8. QString objName = pSender->objectName();
    9.  
    10.  
    11. QList<QListWidgetItem*> items = ui->profileSettingsList->selectedItems();
    12. foreach(QListWidgetItem* item, items)
    13. {
    14. QString profileName;
    15. profileName.append(item->text());
    16.  
    17. QList<QListWidgetItem*> items = ui->slotList->selectedItems();
    18. foreach(QListWidgetItem* item, items)
    19. {
    20. QString slotName;
    21. slotName.append(item->text());
    22.  
    23. settings.beginGroup(profileName);
    24. settings.beginGroup(slotName);
    25. settings.setValue(objName ,keySequence);
    26. settings.endGroup();
    27.  
    28. qDebug() << objName;
    29. }
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 


    This will be a reusable function for capturing configurations in real time and easily expandable later on to different widgets!
    Attached Images Attached Images
    Last edited by Prodeine; 7th March 2019 at 00:19.

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,929
    Thanks
    229
    Thanked 615 Times in 605 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Referencing a Widget with a command

    You seem to still be a little confused about QString. There is no need to call QString::append() to assign a value to a QString. The function is used if you want to add more text to the end of a QString that may already contain some text. Just make a simple assignment:

    Qt Code:
    1. QString profileName = item->text();
    2.  
    3. QString slotName = item->text();
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #8
    Join Date
    Mar 2019
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Referencing a Widget with a command

    Thank you for checking on that, I guess it was just dead code from an old version of the function. I will update it when I get home!

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,190
    Thanks
    36
    Thanked 1,489 Times in 1,441 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Referencing a Widget with a command

    Btw, a slot name like on_slotHotkey_keySequenceChanged() can easily be confused with a "connect by name" slot for an object called "slotHotKey" and a signal called "keySequenceChanged"

    If you accidentally rename one of your UI file objects to "slotHotKey" and it has a "keySequenceChanged" signal then it will also get connected to that slot during "setupUi".

    Cheers,
    _

Similar Threads

  1. Referencing QCA Using QMake
    By wswartzendruber in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2009, 09:09
  2. Style Sheets: re-using/referencing builtin pixmaps
    By chezifresh in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2009, 20:18
  3. Referencing Parent Widget from Child
    By taylor34 in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2006, 16:13

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.