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

  • Home
  • SEARCH
  • 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 5986811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:43:33+00:00 2026-05-22T22:43:33+00:00

I need to create several custom event classes for a Qt application. Right now,

  • 0

I need to create several custom event classes for a Qt application.

Right now, it looks like I will need to implement the following event type registration code for each event class:

class MyEvent : public QEvent
{
public:
    MyEvent() : QEvent(registeredType())
    {
    }

    static QEvent::Type eventType;

private:
    static QEvent::Type registeredType();
}

QEvent::Type MyEvent::eventType = QEvent::None;

QEvent::Type MyEvent::registeredType()
{
    if (eventType == QEvent::None)
    {
        int generatedType = QEvent::registerEventType();
        eventType = static_cast<QEvent::Type>(generatedType);
    }
    return eventType;
}

Any suggestions on how I can simplify this, or at least hide it with a macro?

  • 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-22T22:43:33+00:00Added an answer on May 22, 2026 at 10:43 pm

    That’s what templates are for. They can be used with constant integral parameters, which need to be known at compile time too:

    enum EventNames { UpdateEvent,... }
    
    template<EventNames E>
    class MyEvent : public QEvent
    {
    public:
        MyEvent() : QEvent(registeredType())
        {
        }
    
        static QEvent::Type eventType;
    
    private:
        static QEvent::Type registeredType();
    }
    

    The common code lokes like this:

    template<EventNames E>
    QEvent::Type MyEvent<E>::registeredType()
    {
        if (eventType == QEvent::None)
        {
            int generatedType = QEvent::registerEventType();
            eventType = static_cast<QEvent::Type>(generatedType);
        }
        return eventType;
    }
    

    Static initialization (beware!) looks like this:

    QEvent::Type MyEvent<UpdateEvent>::eventType = QEvent::None;
    

    The code specific for each event type can be implemented as template specialization then.

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

Sidebar

Related Questions

I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create several applications that all share a Microsoft SQL Server database.
I need to create simple reusable javascript object publishing several methods and parameterized constructor.
I have several web services I need to create for them some testing mechanism
I need to validate an email address which will be transmitted to several possible
I need to create several combo boxes to represent enumeration values. I want the
I need to create custom urls for my users, for ex: www.example.com/alpha, www.example.com/beta. I
How to create custom rename dialog for my type of resource in Eclipse (basic
I am developing an application which utilizes a custom network protocol over TCP. Several
I need to create a historical timeline starting from 1600's to the present day.

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.