Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt3, ColorDialog, and SLOT



  • I'm modifying a Qt3 app to add ColorDialog through a configuration menu.
    There are two items to select the colour for - both colours are saved to the configuration file and can be read to set the required colours if the app is restarted.

    On choosing the colour in ColorDialog, the background colour will change immediately, but the button colour needs a restart of the app to read the new colour from the configuration file. I would like that to show immediately.

    Currently:

    background.cpp:

    background::background ()
    {
    // other config menu entries

    m->insertItem ( i18n ("Background color"), this, SLOT ( ColorW1() ) );
    m->insertItem ( i18n ("Button color"), this, SLOT ( ColorW2() ) );
    }

    void background::ColorW1() // background colour chosen through the configuration menu
    {
    // ColorDialog stuff
    // write value to configuration file
    // and effect an immediate colour change:
    setPaletteBackgroundColor ( QColor( b ) );
    }

    void background::ColorW2() // button colour chosen through the configuration menu
    {
    // ColorDialog stuff
    // write value to configuration file
    // but ..
    // can't set PaletteBackgroundColor because it's in button
    }

    ToDo:

    What I'd like to do is move background::ColorW2() to button.cpp so that PaletteBackgroundColor will apply there:

    void button::ColorW2() // button colour chosen through the configuration menu
    {
    // ColorDialog stuff
    // write value to configuration file
    // and effect an immediate colour change:
    setPaletteBackgroundColor ( QColor( k ) );
    }

    Is it possible to set parameters for the object and slot to achieve that?
    m->insertItem ( i18n ("Button color"), ???, SLOT ( ColorW2() ) );

    Or, are there any other solutions?



  • @Ray-V

    Qt3??!! Is there something, that prevents you from upgrading at least to Qt5?

    What type of class is background? What is m?

    @Ray-V said in Qt3, ColorDialog, and SLOT:

    What I'd like to do is move background::ColorW2() to button.cpp so that PaletteBackgroundColor will apply there:

    You don't need to move it. Just send the picked color from your ColorDialog to your (custom) button class (just guessing it is one) by using e.g. signals and slots.

    Where do you create your buttons? Are they members of background? If so, you could use their pointers to access them and apply the new background color.



  • @Pl45m4

    Qt3??!! Is there something, that prevents you from upgrading at least to Qt5?

    Yes - KDE3 is based on Qt3, and modifying this app for a real world application is a useful introduction for me to generate some Qt development experience.
    I'm modifying an existing app, not creating a new one.

    What is m?

    The menu.
    KPopupMenu *m = tray->contextMenu();

    What I'd like to do is move background::ColorW2() to button.cpp so that PaletteBackgroundColor will apply there:

    I thought that would be the way to go - re: QPopupMenu, insertItem():
    "This default version inserts a menu item with the text text, the accelerator key accel, an id and an optional index and connects it to the slot member in the object receiver."
    So if the receiver is where the PaletteBackgroundColor is set, I could use setPaletteBackgroundColor ( QColor( k ) ); as I had done for the background colour.
    I presumed from that that the slot wouldn't change, but what syntax would I use to replace the 'this' receiver?

    You don't need to move it. Just send the picked color from your ColorDialog to your (custom) button class (just guessing it is one) by using e.g. signals and slots.

    Well, if I understand you, that's what I've done for the background colour -> background class. But doing that, as currently set up, for the button colour just overrides the background colour.

    Are you suggesting that the PaletteBackgroundColor can be sent to button::button() - how do I do that? I thought variables couldn't be sent between functions.

    Where do you create your buttons? Are they members of background? If so, you could use their pointers to access them and apply the new background color.

    The PaletteBackgroundColor for the buttons is set in button.cpp - button::button()



  • @Ray-V said in Qt3, ColorDialog, and SLOT:

    QPopupMenu, insertItem():

    Ah, I see. I started with Qt4, so I'm not familiar with QPopupMenu. It's deprecated now and got removed with Qt5 or maybe even with the Qt4 release.

    Are you suggesting that the PaletteBackgroundColor can be sent to button::button() - how do I do that? I thought variables couldn't be sent between functions.

    Yes kind of. Not between functions directly, but you can send your color (in any form, as string or Color or QPalette, whatever...) with a signal to your button instance.
    So you emit the signal on palette change, send the new colors to your button class and assign the new background color.

    // c = new, picked bg color
    Q_EMIT paletteChanged(c);
    

    The signal could look like this:

    void paletteChanged(QColor);
    

    In your button class, you add a slot, that takes a QColor and set the passed color to your button.

    void button::assignBGColor(QColor c){
    // DO STUFF WITH YOUR COLOR
    }
    

    Then you only need to connect the class that makes the changes with your button instances.

    connect(this, SIGNAL(paletteChanged(QColor)), YOUR_BUTTON, SLOT(assignBGColor(QColor)));
    

    EDIT: After writing and re-thinking this, I fear I misunderstood what you are trying to do :-)

    The slot, that's defined in insertItem is the slot that is called, when you activate that item.

    So, back to your initial question:
    Yes, you can change the receiver of your button action.
    You can replace this and the slot ColorW2 with any object and slot you have available, but in order to see some changes, you need to make sure, that the button you see, is an instance of your button class and not any menu item...
    What "buttons" do you want to change? The "buttons" from your menu, some QPushButtons or do you have some separate "buttons" defined by button class?


  • Qt Champions 2019

    @Ray-V said in Qt3, ColorDialog, and SLOT:

    and modifying this app for a real world application is a useful introduction for me to generate some Qt development experience

    It would be better to start with Qt5. There are quite some differences between Qt3 and Qt5, so you have to adapt later, when you switch to Qt 5.



  • @Pl45m4

    What "buttons" do you want to change?

    some separate "buttons" defined by button class?

    You can replace this and the slot ColorW2 with any object and slot you have available, but in order to see some changes, you need to make sure, that the button, you see, is an instance of your button class and not any menu item...

    Doesn't button::ColorW2() in button.cpp do that?

    In which case insertItem would be:
    m->insertItem ( i18n ("Button color"), <receiver>, SLOT ( ColorW2() ) );

    Building the app with
    m->insertItem ( i18n ("Button color"), this, SLOT ( ColorW2() ) );
    generates a run time error: QObject::connect: No such slot background::ColorW2()
    which was expected given where the menu is.

    So <what goes here?> so that QObject::connect sees the slot button::ColorW2()
    m->insertItem ( i18n ("Button color"), <what goes here?>, SLOT ( ColorW2() ) );

    I'll also see what I can do with the emit solution. As it's the immediate change of colour that's the issue, that will probably work just as well.



  • @jsulm

    when you switch to Qt 5.

    That won't be happening for the foreseeable future.

    My desktops are KDE3 and TDE, both of which are Qt3 based, and when I say 'Qt development' I mean being able to modify the code a bit - my brain isn't wired for serious programming.

    I can usually manage to find the information I need through precedent, either in the existing code, or documentation, or examples, or forums. But in this case, any information I can find predominately relates to the receiver being 'this', or less often, a specific value being sent.

    I figured that if what I'd done for the background can work for the SLOT being a function in the 'this' class, then maybe there is a way to access a SLOT in another class. So I thought I'd ask the question at the best repository of Qt knowledge.

    Do you have an answer to my question?


  • Qt Champions 2019

    @Ray-V said in Qt3, ColorDialog, and SLOT:

    to access a SLOT in another class.

    Of course:

    connect(someObject, SIGNAL(someSignal()), someOtherObject, SLOT(someOtherSignal()));
    


  • @jsulm

    connect(someObject, SIGNAL(someSignal()), someOtherObject, SLOT(someOtherSignal()));

    Thank you for your reply, but it's generic.

    The point I'm trying to make is that I don't know, nor can I find out, what the actual typed character string will be for 'someOtherObject' in the scenario I've set out.

    Where the SIGNAL is in the background class, and I want to connect to the ColorW2() SLOT in the button class, what do I actually type in for 'someOtherObject'?



  • @Ray-V
    You do connect() signal-to-slot on instances of the classes, not somehow on the classes themselves. Usually when you create the slot objects. You don't need to know the " actual typed character string", just which object sends the signal and which object receives the signal. You may have many connect()s if you have multiple objects sending or receiving the signal.



  • @Ray-V said in Qt3, ColorDialog, and SLOT:

    what do I actually type in for 'someOtherObject'?

    Your button instance.
    Do you want to repaint one or multiple buttons?

    If you have only one, you can put your button instance (the pointer to your button) instead of this in your insertItem function. Then, if you have your W2 slot in your button class, it will run from there, so you can change the button's background directly.


Log in to reply