How to remove caret cursor in Gtk entry - c++

I have a Gtk entry. I need to completely remove this caret cursor, how can I do this? I searched for information about this for a long time but found only how to remove the blinking of Gtk entry.

Until you find a better one use CSS.
Just set the caret-color background to the same background-color of the entry:
main.c
#include <gtk/gtk.h>
int main ( void )
{
GtkWidget *window;
GtkWidget *grid;
GtkWidget *entry;
/// ***
gtk_init ( NULL, NULL );
/// ***
window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title ( GTK_WINDOW ( window ), "Hello There!" );
gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 100 );
g_signal_connect ( window, "destroy", gtk_main_quit, NULL );
///***
grid = gtk_grid_new();
gtk_container_add ( GTK_CONTAINER ( window ), grid );
///***
entry = gtk_entry_new();
g_object_set ( entry, "margin-top", 30, NULL );
g_object_set ( entry, "margin-left", 20, NULL );
gtk_grid_attach ( GTK_GRID ( grid ), entry, 0, 0, 1, 1 );
/// ***
gtk_widget_show_all ( window );
gtk_main();
}
CSS:
window
{
background-color: red;
}
entry
{
background-color: yellow;
caret-color: yellow;
}
rezult:

Related

QGraphicsLinearLayout not aligning widgets as expected

I have got a QGraphicsScene that I have added a QGraphicsWidget.
I initially setup the widget this way
AGraphicsWidget::AGraphicsWidget( QGraphicsItem* parent, QGraphicsScene* scene )
: QGraphicsWidget( parent )
, m_mainLayout( new QGraphicsLinearLayout( Qt::Vertical ) )
, m_titleLabel( new QLabel( "Title" ) )
, m_executionPin( new NodeExecutionPin() )
{
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
setGraphicsItem( this );
setFlag( ItemIsMovable, true );
m_mainLayout->setInstantInvalidatePropagation( true );
auto* labelProxy = scene->addWidget( m_titleLabel );
auto* secondLabel = scene->addWidget( new QLabel( "Subtitle" ) );
auto* button = scene->addWidget( new QPushButton( "Another" ) );
m_mainLayout->addItem( labelProxy );
m_mainLayout->addItem( secondLabel );
m_mainLayout->addItem( button );
setLayout( m_mainLayout );
}
This setup does not do what I expect which would be a vertically aligned set of widgets instead I get this
However when I add a text edit as the last element like this
AGraphicsWidget::AGraphicsWidget( QGraphicsItem* parent, QGraphicsScene* scene )
: QGraphicsWidget( parent )
, m_mainLayout( new QGraphicsLinearLayout( Qt::Vertical ) )
, m_titleLabel( new QLabel( "Title" ) )
, m_executionPin( new NodeExecutionPin() )
{
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
setGraphicsItem( this );
setFlag( ItemIsMovable, true );
m_mainLayout->setInstantInvalidatePropagation( true );
auto* labelProxy = scene->addWidget( m_titleLabel );
auto* secondLabel = scene->addWidget( new QLabel( "Subtitle" ) );
auto* button = scene->addWidget( new QPushButton( "Another" ) );
auto* edit = scene->addWidget( new QTextEdit() );
m_mainLayout->addItem( labelProxy );
m_mainLayout->addItem( secondLabel );
m_mainLayout->addItem( button );
m_mainLayout->addItem( edit );
setLayout( m_mainLayout );
}
It works as expected, I get the nice vertical layout, I get the same non alignment as above if I try and nest any QGraphicsLinearLayouts any further where they will overlap each other, what is happening here?
Thanks in advance.

gtkmm: How to make stretched widget normal size?

As you can see from the screenshot, the button and widget are stretched. How to make them normal size? Here is the main code.
#include <gtkmm.h>
#include "w.h"
int main ( int argc, char **argv )
{
auto app = Gtk::Application::create ( argc, argv, "org.gtkmm.test" );
Gtk::Window window;
Gtk::Button btn;
Gtk::Box box(Gtk::ORIENTATION_HORIZONTAL);
W w;
btn.set_label ( "test" );
window.set_default_size ( 200, 200 );
box.pack_start ( btn, false, false, 0 );
box.pack_start ( w, false, false, 0 );
window.add ( box );
btn.show();
box.show();
w.show();
return app->run ( window );
}

Visual artifacts when style is applied to overlapping Qt PushButtons

In the application I'm developing, I've got sets of QPushButtons that overlay each other like so:
resize(300, 300);
QRect geometry( 100, 100 , 100 , 100 );
m_button[0] = new QPushButton( this );
m_button[0]->setGeometry( geometry );
m_button[0]->setText( "Button 1" );
m_button[1] = new QPushButton( this );
m_button[1]->setGeometry( geometry );
m_button[1]->setText( "Button 2" );
m_button[1]->raise();
Result:
But when the follows style is added at application level
QApplication app(argc, argv);
app.setStyleSheet( "QPushButton{ border-width: 1px; border-color: blue; border-style: solid;}" );
the buttons are rendered as like so:
What would be the reason for this artefact, and how can I workaround it.
Information about my application
The overlapping buttons are keys belonging to different character sets of a virtual keyboard. These keys are raised or lowered depending on the current character set.
You want to use a QStackedWidget and place your QPushButtons in that stack. Then you just need to raise the button you want with QStackedWidget::setCurrentIndex or QStackedWidget::setCurrentWidget.
Edit: sample code:
resize(300, 300);
QStackedWidget* stack = new QStackedWidget(this);
stack->setGeometry( 100, 100 , 100 , 100 );
m_button[0] = new QPushButton( this );
m_button[0]->setText( "Button 1 );
stack->addWidget(m_button[0]);
m_button[1] = new QPushButton( this );
m_button[1]->setText( "Button 2" );
stack->addWidget(m_button[1]);
stack->setCurrentWidget(m_button[1]); //or stack->setCurrentIndex(1);

How to set Qt QStateMachine animation duration

I'm trying to learn the Qt framework. My QStateMachine code does the correct thing (pressing the button makes the chat window popup change size).
I can't change the speed of the animation to get a nice visual transition.
Any suggestions?
Here's the code:
MainWindow::MainWindow()
{
widget.setupUi( this );
// chat window - Chat button opens
ChatWindowClosedState = new QState();
ChatWindowOpenState = new QState();
ChatWindowOpenGeometry = widget.groupBox->geometry();
ChatWindowClosedGeometry = widget.pushButton->geometry();
ChatWindowClosedGeometry.translate( -ChatWindowClosedGeometry.width(), 0 );
ChatWindowClosedState->assignProperty( widget.groupBox, "geometry", ChatWindowClosedGeometry );
ChatWindowOpenState->assignProperty( widget.groupBox, "geometry", ChatWindowOpenGeometry );
ChatWindowCloseTransition = ChatWindowClosedState->addTransition( widget.pushButton, SIGNAL( clicked() ), ChatWindowOpenState );
ChatWindowCloseAnimation = new QPropertyAnimation( widget.pushButton, "geometry" );
ChatWindowCloseAnimation->setDuration( 5000 );
ChatWindowCloseTransition->addAnimation( ChatWindowCloseAnimation );
ChatWindowOpenTransition = ChatWindowOpenState->addTransition( widget.pushButton, SIGNAL( clicked() ), ChatWindowClosedState );
ChatWindowOpenAnimation = new QPropertyAnimation( widget.pushButton, "geometry" );
ChatWindowOpenAnimation->setDuration( 5000 );
ChatWindowOpenTransition->addAnimation( ChatWindowOpenAnimation );
machine = new QStateMachine( this );
machine->addState( ChatWindowClosedState );
machine->addState( ChatWindowOpenState );
machine->setInitialState( ChatWindowClosedState );
machine->start();
}
The code
ChatWindowOpenAnimation = new QPropertyAnimation( widget.pushButton, "geometry" );
should be
ChatWindowOpenAnimation = new QPropertyAnimation( widget.groupBox, "geometry" );
The animation was being applied to the wrong widget.

Qt QAbstractButton setDown interferes with grabMouse

I have some weird behaviour in Qt that seems like a defect. I'd like to know if anybody has a good workaround.
I have a popup widget that contains many buttons in it. The user activates the popup by pressing the mouse button down. The popup widget calls grabMouse when shown. It gets all the mouse events. As it rolls over a button it calls setDown(true) on the button. Now however, when the mouse button is released the popup widget does not get the mouseReleaseEvent, that goes to the button.
That is, calling setDown(true) on a button causes the button to steal mouse events, bypassing the grabMouse in the popup widget.
I've looked at the source code for setDown but I can't see anything there that would do it directly. I also notice however that sometimes a button gets a hover event, sometimes not. I would assume it would never get those events when the mouse is grabbed.
//g++ -o grab_lost grab_lost.cpp -lQtCore -lQtGui -I /usr/include/qt4/ -I /usr/include/qt4/QtCore -I /usr/include/qt4/QtGui
/**
Demonstrates the defect of losing the mouse. Run the program and:
1. Press mouse anywhere
2. release in purple block (not on X)
3. Release message written (GrabLost receives the mouseReleaseEvent)
For defect:
1. Pree mouse anywhere
2. Release inside the X button
3. button is clicked, no release message (GrabLost does not get the mouseReleaseEvent)
*/
#include <QWidget>
#include <QPushButton>
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
class GrabLost : public QWidget
{
QPushButton * btn;
public:
GrabLost( QWidget * parent = 0)
: QWidget( parent, Qt::Popup )
{
btn = new QPushButton( "X", this );
setMouseTracking( true );
}
protected:
void showEvent( QShowEvent * ev )
{
QWidget::showEvent( ev );
grabMouse();
}
void closeEvent( QCloseEvent * ev )
{
releaseMouse();
QWidget::closeEvent( ev );
}
void hideEvent( QHideEvent * ev )
{
releaseMouse();
QWidget::hideEvent( ev );
}
void mouseReleaseEvent( QMouseEvent * ev )
{
qDebug( "mouseRelease" );
close();
}
void mouseMoveEvent( QMouseEvent * ev )
{
QWidget * w = childAt( ev->pos() );
bool ours = dynamic_cast<QPushButton*>( w ) == btn;
btn->setDown( ours );
}
void paintEvent( QPaintEvent * ev )
{
//just to show where the widget is
QPainter pt( this );
pt.setPen( QColor( 0,0,0 ) );
pt.setBrush( QColor( 128,0,128) );
pt.drawRect( 0, 0, size().width(), size().height() );
}
};
class GrabMe : public QWidget
{
protected:
void mousePressEvent( QMouseEvent * ev )
{
GrabLost * gl = new GrabLost();
gl->resize( 100, 100 );
QPoint at( mapToGlobal( ev->pos() ) );
gl->move( at.x() - 50, at.y() - 50 );
gl->show();
}
};
int main( int argc, char** argv )
{
QApplication app( argc, argv );
GrabMe * gm = new GrabMe();
gm->move( 100, 100 );
gm->resize( 300, 300 );
gm->show();
app.exec();
return 0;
}
I've entered the defect at the Nokia DB. I'm giving it about a 95% chance that they close it as "works as intended".
For those of you that need a solution nonetheless you'll have to use event filters and create your own grabbing. Basically install an event filter for every child widget and propagate the mouse events to the parent.
Note in the above code that the right mouse button doesn't work even if you don't call setDown.