13

Is there any way to configure the clang-format tool to skip my Qt::connect function calls ? There are several connects in my constructors which looks like these:

connect( m_Job, SIGNAL( error( const QString&, const QString& ) ),  this, SLOT( onError( const QString&, const QString& ) ) );
connect( m_Job, SIGNAL( message( const QString& ) ),                this, SLOT( onMessage( const QString& ) ) );
connect( m_Job, SIGNAL( progress( int, int ) ),                     this, SLOT( onProgress( int, int ) ) );

but after I run the formatting tool it makes it less readable:

connect( m_Job, SIGNAL( error(const QString&, const QString&)), this, SLOT( onError(const QString&, const QString&)) );
connect( m_Job, SIGNAL( message(const QString&)), this, SLOT( onMessage(const QString&)) );
connect( m_Job, SIGNAL( progress(int, int)), this, SLOT( onProgress(int, int)) );
jtv
  • 128
  • 5
p.i.g.
  • 2,559
  • 1
  • 19
  • 38

3 Answers3

28

Use // clang-format off and // clang-format on to make it skip code sections.

// clang-format off
// Don't touch this!
connect( m_Job, SIGNAL( error( const QString&, const QString& ) ),  this, SLOT( onError( const QString&, const QString& ) ) );
connect( m_Job, SIGNAL( message( const QString& ) ),                this, SLOT( onMessage( const QString& ) ) );
connect( m_Job, SIGNAL( progress( int, int ) ),                     this, SLOT( onProgress( int, int ) ) );
// clang-format on
// Carry on formatting
RedX
  • 13,656
  • 1
  • 46
  • 69
1

You can use new signal slot syntax for better readable. Its look much simplier

connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue);

Szymson
  • 829
  • 1
  • 10
  • 26
  • Another benefit of this syntax, your compiler will complain of non-existant/incompatible syntax/slot whereas the old syntax produces a runtime error – Alejandro Díaz Oct 21 '15 at 11:36
  • Okay, I know but it would be nice to use a table-like layout for this purpose like in my first example. A table with headers: sender, signal, receiver, slot. If I have a lot of connections and I don't use this format it makes it difficult to read. I use Qt5. – p.i.g. Oct 21 '15 at 11:41
0

As an aside: You should be normalizing the signal/slot signatures. Thus, the references and const-references are not needed, the signature normalization code within Qt simply removes them. You also don't need the third parameter if it's this.

Your code should look as follows:

connect(m_Job, SIGNAL(error(QString,QString)), SLOT(onError(QString,QString)));
connect(m_Job, SIGNAL(message(QString)), SLOT(onMessage(QString)));
connect(m_Job, SIGNAL(progress(int,int)), SLOT(onProgress(int,int)));

If you insist, there certainly can be spaces between the parameter types, at some runtime cost of course since the normalization code isn't a no-op anymore.

You can also leverage QMetaObject::connectSlotsByName to get rid of explicit connections. This requires that m_Job is a child of this, and has a name. For example:

class Foo : public Q_OBJECT {
  Job m_job;
  Q_SLOT void on_job_error(const QString&, const QString&);
  Q_SLOT void on_job_message(const QString&);
  Q_SLOT void on_job_progress(int, int);
public:
  Foo(QObject * parent = 0) :
    QObject(parent),
    m_job(this)
  {
    m_job.setObjectName("job");
    QMetaObject::connectSlotsByName(this);
  }
};

The slots with names having the pattern on_name_signal will be automatically connected by connectSlotsByName. The name is the name of the sender object, and signal is the name of the signal.

Finally, the excessive whitespace can make your code harder, not easier, to read. This is not an issue of style, but of simple physiology. Fovea centralis is about 2 angular degrees in diameter. One angular degree of vision is about the width of your thumb at arms' length. Reading code with excessive whitespace requires more saccades/fixations to relocate your central vision along the line of code. Figure 0.15-0.2s needed to process each fixation's worth of data and integrate it with your mental model of the code you're reading. It's all measurable.

As an anecdote, not medical advice: I can't read dense sheet music without +0.5 glasses on my nose. My vision is otherwise completely normal. YMMV.

Kuba hasn't forgotten Monica
  • 88,505
  • 13
  • 129
  • 275