Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Reduce duplicate codes of connect
Forum Updated to NodeBB v4.3 + New Features

[Solved]Reduce duplicate codes of connect

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on 22 Apr 2013, 07:16 last edited by
    #1

    I always deal with codes like these

    connection
    @
    connect(ui->slider_interval_, SIGNAL(valueChanged(int)), this, SLOT(change_spinbox_pae_interval(int)));
    connect(ui->spinbox_interval_, SIGNAL(valueChanged(int)), this, SLOT(change_slider_pae_interval(int)));
    @

    slot
    @
    void colorBalanceWidget::change_slider_pae_interval(int value)
    {
    if(ui->slider_interval_->value() != ui->spinbox_interval_->value()){
    ui->slider_min_slope_->setValue(value);
    }
    }

    void colorBalanceWidget::change_spinbox_pae_interval(int value)
    {
    if(ui->slider_interval_->value() != ui->spinbox_interval_->value()){
    ui->spinbox_interval_->setValue(value);
    }
    }
    @

    Do you have a solution to avoid these duplicate codes?
    The best I can do is encapsulate the codes and call them
    in different slots.Thanks

    @
    template<typename T, typename U, typenmae V, typename W>
    void set_value(T *fir, U *sec, V *target, W value)
    {
    if(fir->value() != sec->value()){
    target->setValue(value);
    }
    }@

    don't looks like a good solution

    1 Reply Last reply
    0
    • C Offline
      C Offline
      code_fodder
      wrote on 22 Apr 2013, 08:12 last edited by
      #2

      Are you trying to use one slot for multiple purposes?

      To get around this issue I have created my signals with a "signal type" variable (not actually that name) then you can effectively use the same slot for many purposes (if that is what you want to do)

      For example the signal function could be like:
      @some_signal(eSomeEnumType type, int val);@

      The slot would take the same types:
      @void slot_to_do_thing(eSomeEnumType type, int val)
      {
      // (if you have many options use switch)
      if (eSlider == type)
      {
      do thing to slider....
      }
      else if (eSpin == type)
      {
      do thing to spinbox
      }
      }@

      Edit:
      Forgot to add: You could even pass in your template type in if you wanted to reduce the if/switch statements. Note: if this is a signal sent between different threads you have to register the eNum/Template type with the following line of code (I usually stick this in at start-up time:
      @qRegisterMetaType<someTypeOrEnum>("someTypeOrEnum");@

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 22 Apr 2013, 09:14 last edited by
        #3

        it's safe to do to the following:
        @
        connect(ui->slider_interval_, SIGNAL(valueChanged(int)), ui->spinbox_interval_, SLOT(setValue(int)));
        connect(ui->spinbox_interval_, SIGNAL(valueChanged(int)), ui->slider_interval_, SLOT(serValue(int)));
        @
        you wont start a infinite loop with such connect-statements ;)
        Since Qt already only triggers the signals only when the value changes.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0

        2/3

        22 Apr 2013, 08:12

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved