Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6565655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:07:16+00:00 2026-05-25T14:07:16+00:00

I need to implement a mechanism of highlighting duplicated values. Values are edited through

  • 0

I need to implement a mechanism of highlighting duplicated values. Values are edited through delegate depending on the value type (string – line edit, long and big decimal – spin boxes). Currently, I implemented this feature with help of additional class which stores all values and their counts in two “parallel” lists. And after adding a new value I increase its count number (or decrease when repeated value is removed), but this solution seems to be too bulky. Do you guys have any other ideas on highlighting in setModelData(...) method of QItemDelegate?

/**
* Stores a delegates' existing values
*/
private final class DelegateValuesStorage {
    private final List<Object> values = new ArrayList<Object>();
    private final List<Integer> counts = new ArrayList<Integer>();

    ....

    //Add value or increase a count if exists
    public void add(final Object value) {
        if(values.contains(value)) {
            final int valueIndex = values.indexOf(value);
            final int oldCount = counts.get(valueIndex);
            counts.remove(valueIndex);
            counts.add(valueIndex, oldCount + 1);
        } else {
            values.add(value);
            counts.add(1);
        }
    }

    ....

    //Decrease a count or remove value if it doesn't exist anymore
    public void decreaseCount(final Object value) {
        if(value == null) {
            return;
        }
        final int index = values.indexOf(value);
        if(index >= 0) {
            final int oldCount = counts.get(index);
            if(oldCount >= 2) {
                counts.remove(index);
                counts.add(index, oldCount - 1);
            } else {
                values.remove(index);
                counts.remove(index);
            }
        }
    }

/**
* Delegate
*/
private class ConcreteDelegate extends QItemDelegate {

    private final DelegateValuesStorage values = new DelegateValuesStorage(); 

    ...

    @Override
    public void setModelData(final QWidget editor, final QAbstractItemModel model, final QModelIndex index) {
        if(editor instanceof ValEditor) { // ValEditor is an abstraction of line edit and spin box over values' data types

            final Object value = ((ValEditor) editor).getValue();
            model.setData(index, value, Qt.ItemDataRole.UserRole);
            final String newData = (value == null) ? "" : String.valueOf(value);
            values.add(newData);
            final String oldData = (String) model.data(index, Qt.ItemDataRole.DisplayRole);
            values.decreaseCount(oldData);


            model.setData(index, newData, Qt.ItemDataRole.DisplayRole);
            model.setData(index, new QColor(0, 0, 0), Qt.ItemDataRole.ForegroundRole);
            redrawItems(model); // runs through values and colors them red if count >= 2; or black if count == 1

        } else {
            super.setModelData(editor, model, index);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:07:17+00:00Added an answer on May 25, 2026 at 2:07 pm

    I usually use maps for those kinds of tasks:

    private final class DelegateValuesStorage {
        private final Map<Object, Integer> values = new HashMap<Object, Integer>();
    
        ....
    
        //Add value or increase a count if exists
        public void add(final Object value) {
           Integer count = values.get(value);
           if (count == null) {
              values.put(value, 1);
           } else {
              values.put(value, count + 1);
           }
        }
    
        ....
    
        //Decrease a count or remove value if it doesn't exist anymore
        public void decreaseCount(final Object value) {
            if(value == null) {
                return;
            }
            Integer count = values.get(value);
            if (count == null) {
                // decreasing a "new" value - could be an error too
                return;
            }
            if (count <= 1) {
                // remove the value from the map
                values.remove(value);
            } else {
                values.put(value, count - 1);
            }
        }
    }
    

    Highlighting now should be enabled if

    values.get(value) > 1
    

    is true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For various reasons, I need to implement a type caching mechanism in C#. Fortunately,
I need to implement some form of communication mechanism in my application, to send
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism in Java.
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism. In the
I need to implement a simple logging mechanism for my web application. I have
I need to implement a mechanism where I can initialize a vector of my
I need a mechanism to implement the following scenario: two or more threads need
I need to implement a heartbeat-mechanism that sends a 'touch'-message to an external service
I need to implement a notification mechanism for a system that has one manager
I need to implement a very specific demo mechanism. It must not expire after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.