SoFunction
Updated on 2025-04-18

Use of QCommandLinkButton control in Qt

Introduction

QCommandLinkButton is a class of the QtWidgets module in the Qt framework. It provides a control that combines text labels and button functions, similar to the Command Link Button on the Windows platform. This type of button is usually used for operations that require a larger click area or are more visually striking, such as "install" and "download" buttons.

1. Overview

QCommandLinkButton inherits from QAbstractButton, which provides the ability to set description and command text. The description text is usually longer and is used to provide additional information about the button's function, while the command text is shorter, usually located below or next to the description text, as the main label of the button.

2. Features and Attributes

1. Properties

  • Description: Used to provide additional information about button operation.
QCommandLinkButton *button = new QCommandLinkButton(this);  
button->setDescription("Click here to learn more about the product.");
  • Command Text: The main label of the button, usually shorter, is used to indicate the function of the button.
button->setText("learn more");
  • Icon: The icon can be set like other Qt buttons.
button->setIcon(QIcon(":/path/to/your/"));  
// If you need to set the icon sizebutton->setIconSize(QSize(32, 32));
  • Text Alignment: Descriptive text and command text alignment.

QCommandLinkButton does not have a direct setAlignment method to set the alignment of description text and command text respectively. However, you can control the alignment of text through a style sheet (QSS), but this usually affects the text layout of the entire button. For more granular controls, subclassing and custom drawing logic may be required.

However, if you just want to simply adjust the position of the text inside the button (although not a direct alignment setting), you can indirectly influence it by setting the button's margin and padding.

  • Automatic resize (Size Policy): Automatically adjust the size of the button according to the content.

The sizePolicy of QCommandLinkButton is usually QSizePolicy::Expanding (or similar), which means it will automatically resize based on the content, but it will also be subject to the layout manager. Usually, you don't need to explicitly set the size policy unless you have special needs.

However, if you do need to set it explicitly, you can do this:

button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

But please note that this may conflict with the behavior of the layout manager.

2. Style

The appearance of QCommandLinkButton can be highly customized through Qt style sheets (QSS), including colors, fonts, margins, etc.

button->setStyleSheet(  
    "QCommandLinkButton {"  
    "    color: blue;"           // Set text color    "    font-weight: bold;"      // Set font bold    "    padding: 10px;"          // Set inner margin    "    border: 2px solid gray;" // Set borders    "}"  
    "QCommandLinkButton:hover {"  
    "    color: red;"            // Text color when hovering    "    background-color: #f0f0f0;" // Hover background color    "}"  
    "QCommandLinkButton:pressed {"  
    "    background-color: lightgray;" // The background color when pressed    "}"  
);

Note that QCommandLinkButton's stylesheets may be affected by their internal implementations, especially when it comes to text layout and alignment. In the stylesheet above, I used padding to increase the space inside the button, but be aware that this does not affect the relative position between the description text and the command text, which is determined by the internal layout logic of the button.

If you need to have more granular control over text layout, such as setting the alignment of description text and command text separately, you may need to subclass QCommandLinkButton and override its paintEvent method to completely customize the drawing logic. However, this is often complex and requires a deep understanding of Qt's drawing system.

3. Basic usage

1. Introduce necessary header files

First, make sure your project contains the QCommandLinkButton-related header files.

#include <QCommandLinkButton>

2. Create and configure QCommandLinkButton

You can create a QCommandLinkButton object directly in your code and set properties through its member functions.

QCommandLinkButton *button = new QCommandLinkButton(this);  
button-&gt;setText("download"); // Set command textbutton-&gt;setDescription("Click here to download the latest version of the app"); // Set the description textbutton-&gt;setIcon(QIcon(":/path/to/")); // Set iconsbutton-&gt;setIconSize(QSize(32, 32)); // Set icon size  
// Set style sheetbutton-&gt;setStyleSheet("QCommandLinkButton { color: blue; font-weight: bold; }"  
                      "QCommandLinkButton:hover { color: red; }"  
                      "QCommandLinkButton:pressed { background-color: lightgray; }");  
  
// Connect to the signalconnect(button, &amp;QCommandLinkButton::clicked, this, &amp;YourClass::onButtonClicked);

3. Layout Management

Add QCommandLinkButton to the layout to ensure it is in the correct position and size in the interface.

QVBoxLayout *layout = new QVBoxLayout(this);  
layout->addWidget(button);

4. Advanced usage

1. Custom drawing

If you need a more complex custom appearance, you can do it by subclassing the QCommandLinkButton and overwriting the paintEvent method.

class CustomCommandLinkButton : public QCommandLinkButton {  
protected:  
    void paintEvent(QPaintEvent *event) override {  
        QCommandLinkButton::paintEvent(event); // Call base class drawing  
        QPainter painter(this);  
        // Add custom drawing code here    }  
};

2. Dynamic content update

In some cases, you may need to dynamically update the text or description of the button. This can be achieved by calling the setText() and setDescription() methods directly.

button-&gt;setText("renew");  
button-&gt;setDescription("Click here to update to the latest version");

5. Code parsing examples

Here is a complete example showing how to use QCommandLinkButton in a Qt Widgets application.

#include &lt;QApplication&gt;  
#include &lt;QWidget&gt;  
#include &lt;QCommandLinkButton&gt;  
#include &lt;QMessageBox&gt;  
  
class MainWindow : public QWidget {  
    Q_OBJECT  
public:  
    MainWindow(QWidget *parent = nullptr) : QWidget(parent) {  
        // Create QCommandLinkButton        QCommandLinkButton *downloadButton = new QCommandLinkButton(this);  
        downloadButton-&gt;setText("download");  
        downloadButton-&gt;setDescription("Click here to download the latest version of the app");  
          
        // Set the geometric position of the button (here is just a simple example, usually you will use the layout manager)        downloadButton-&gt;setGeometry(50, 50, 200, 50); // x, y, width, height  
          
        // Connect signals and slots        connect(downloadButton, &amp;QCommandLinkButton::clicked, this, &amp;MainWindow::onDownloadButtonClicked);  
    }  
  
public slots:  
    void onDownloadButtonClicked() {  
        // Slot function implementation: the operation performed after clicking the button        QMessageBox::information(this, "download", "Start download the latest version of the app...");  
    }  
};  
  
#include "" // If you are not using qmake and you write the .moc file manually, you need to include this line  
int main(int argc, char *argv[]) {  
    QApplication app(argc, argv);  
  
    MainWindow window;  
    ();  
  
    return ();  
}  
  
// If you are using qmake, you don't need the #include ""Yes// above. Just make sure your .pro file contains QT += widgets and that your class is defined in the .h file// And in your.cppThe file contains the corresponding.hdocument

Notice

1. I used QMessageBox::information() to simulate the results of the download operation. In practical applications, you may need to execute more complex download logic.
2. I set the geometric position of the button (setGeometry) directly in the constructor of MainWindow. In real applications, you usually use a layout manager such as QVBoxLayout, QHBoxLayout, or QGridLayout to manage the layout of widgets, which will allow your application to work well on screens of different sizes and resolutions.
3. If you are using Qt Creator and qmake, you may not need to include the #include "" line. Qt Creator will automatically handle MOC (Meta-Object Compiler) related matters. If you don't use qmake, but instead compile your project manually and don't have .pro files, you may need to handle the MOC manually, but this is rare.
4. Make sure your Qt project file (.pro) contains QT += widgets, because QCommandLinkButton is part of the Qt Widgets module.

Summarize

QCommandLinkButton is a control in the Qt framework for creating command link buttons. It combines the functionality of text tags and buttons and is ideal for operations that require larger click areas or are more visually striking. By setting description text, command text, icons, and style sheets, you can easily customize the appearance and behavior of buttons. In addition, QCommandLinkButton also supports dynamic content updates and custom drawings, providing a high degree of flexibility and scalability.

This is the article about the use of QCommandLinkButton control in Qt. For more related Qt QCommandLinkButton content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!