Qt Widget Essential Properties

enabled

Controls can be enabled or disabled using the following methods:

  • isEnabled(): Returns the current enabled state of the widget
  • setEnabled(bool): Sets whether the widget is interactive
QPushButton *confirmBtn = new QPushButton(this);
QPushButton *cancelBtn = new QPushButton(this);
confirmBtn->setText("Submit");
cancelBtn->setText("Cancel");
cancelBtn->move(0, 100);

qDebug() << "confirmBtn enabled:" << confirmBtn->isEnabled();
cancelBtn->setEnabled(false);
qDebug() << "cancelBtn enabled:" << cancelBtn->isEnabled();

geometry

The move() function only repositions a widget without changing its dimensions. The geometry property encompasses four attributes: position (x, y) and size (width, height).

  • geometry(): Returns a QRect containing position and size
  • setGeometry(QRect) / setGeometry(int x, int y, int width, int height): Sets position and size
QPushButton *firstBtn = new QPushButton(this);
QPushButton *secondBtn = new QPushButton(this);
secondBtn->move(20, 20);

qDebug() << firstBtn->geometry();
qDebug() << secondBtn->geometry();

QRect rect(100, 100, 50, 50);
firstBtn->setGeometry(rect);
qDebug() << firstBtn->geometry();

secondBtn->setGeometry(200, 200, 100, 100);
qDebug() << secondBtn->geometry();

Prank Application Example

QLabel *messageLabel = new QLabel(this);
messageLabel->setText("Do you want to be with me?");
messageLabel->setGeometry(350, 200, 100, 100);

QPushButton *yesBtn = new QPushButton(this);
yesBtn->setText("Yes");

QPushButton *noBtn = new QPushButton(this);
noBtn->setText("No");
yesBtn->setGeometry(650, 500, 90, 30);
noBtn->setGeometry(50, 500, 90, 30);

connect(yesBtn, &QPushButton::clicked, this, [=](){
    Widget::close();
});

connect(noBtn, &QPushButton::clicked, this, [=](){
    int posX, posY;
    posX = rand() % this->geometry().width();
    posY = rand() % this->geometry().height();
    noBtn->move(posX, posY);
});

Clicking "Yes" closes the window, while clicking "No" relocates the button to a random position.

windowIcon

For top-level widgets, the following methods manage the window icon:

  • windowIcon(): Returns the current QIcon object
  • setWindowIcon(const QIcon&): Sets the window icon
QIcon icon("d:/icon.png");
this->setWindowIcon(icon);

Using qrc Resource System

The absolute path approach is cumbersome. Qt provides the qrc mechanism:

  1. Create a new qrc file
  2. Define a prefix (e.g., /images)
  3. Add required image files

When adding files from outside the project directory, Qt will prompt to copy them to the project folder. Confirm this to proceed.

  1. Reference in code:
QIcon icon(":/images/icon.png");
this->setWindowIcon(icon);

windowOpacity

  • windowOpacity(): Returns opacity as float (0.0 to 1.0, where 0.0 is fully transparent)
  • setWindowOpacity(float): Sets the window transparency level
QPushButton *fadeInBtn = new QPushButton(this);
fadeInBtn->setText("+");

QPushButton *fadeOutBtn = new QPushButton(this);
fadeOutBtn->setText("-");
fadeOutBtn->move(0, 200);

connect(fadeInBtn, &QPushButton::clicked, this, [=](){
    float opacity = this->windowOpacity();
    if(opacity >= 1.0) return;
    qDebug() << opacity;
    opacity += 0.1;
    this->setWindowOpacity(opacity);
});

connect(fadeOutBtn, &QPushButton::clicked, this, [=](){
    float opacity = this->windowOpacity();
    if(opacity <= 0.0) return;
    qDebug() << opacity;
    opacity -= 0.1;
    this->setWindowOpacity(opacity);
});

cursor

Mouse cursor appearance when hovering over widgets:

  • cursor(): Returns the currant QCursor object
  • setCursor(const QCursor&): Sets cursor shape for this widget only
  • QGuiApplication::setOverrideCursor(const QCursor&): Sets global cursor for the entire application
QPushButton *waitBtn = new QPushButton(this);
waitBtn->setText("+");

QPushButton *textBtn = new QPushButton(this);
textBtn->setText("-");
textBtn->move(0, 200);

waitBtn->setCursor(QCursor(Qt::WaitCursor));
textBtn->setCursor(QCursor(Qt::IBeamCursor));
QGuiApplication::setOverrideCursor(QCursor(Qt::IBeamCursor));

Ctrl+click on any Qt:: enum value to view the complete cursor shape list in the source code.

Custom Cursor Icons

QPixmap pixmap(":/images/custom_cursor.png");
pixmap = pixmap.scaled(64, 64);
QCursor customCursor(pixmap, 2, 2);
this->setCursor(customCursor);

The hotspot coordinates (2, 2) define the point that registers clicks.

toolTip

  • setToolTip(const QString&): Sets the tooltip text
  • setToolTipDuration(int ms): Sets display duration in milliseconds before auto-dismiss
QPushButton *increaseBtn = new QPushButton(this);
increaseBtn->setText("+");

QPushButton *decreaseBtn = new QPushButton(this);
decreaseBtn->setText("-");
decreaseBtn->move(0, 200);

increaseBtn->setToolTip("Decrease opacity");
increaseBtn->setToolTipDuration(1000);
decreaseBtn->setToolTip("Increase opacity");
decreaseBtn->setToolTipDuration(1000);

focusPolicy

Focus determines which widget receives keyboard input. For instance, an input field gains focus to accept typing.

  • focusPolicy(): Returns the current Qt::FocusPolicy
  • setFocusPolicy(Qt::FocusPolicy): Configures focus acquisition behavior
Value Behavior
Qt::NoFocus Widget never receives keyboard focus
Qt::TabFocus Widget receives focus via Tab key only
Qt::ClickFocus Widget receives focus via mouse click only
Qt::StrongFocus (default) Widget accepts focus via Tab or mouse click
Qt::WheelFocus Like StrongFocus, plus mouse scroll wheel

styleSheet

Qt widgets support CSS-like styling:

this->setStyleSheet("background-color: #333");

Tags: Qt Widget gui properties C++

Posted on Sun, 10 May 2026 01:34:05 +0000 by adamb10