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:
I'm beginner in Qt/C++ programmation and i want to get screenshot of my window using Qt.
#include <qapplication.h>
#include <qpushbutton.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton hello( "Hello world!", 0 );
hello.resize( 100, 30 );
a.setMainWidget( &hello );
hello.show();
return a.exec();
}
Anyone can help me please ?
Here is a short way to take a screenshot of a specific window :
/* Take a screenshot of a window
Notice that window is a: QWidget *window; */
originalPixmap = QPixmap::grabWidget(window);
I am new to Qt and when I tried to compile and run a Qt program from "Foundations of Qt Development " Chapter 7, see
http://www.java2s.com/Code/Cpp/Qt/QGraphicsViewQGraphicsItemandQGraphicsScene.htm
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QGLWidget>
QGraphicsItem *createItem( int x, QGraphicsScene *scene )
{
QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( x+40, 40, 120, 120 ), 0, scene );
rectItem->setPen( QPen(Qt::black) );
rectItem->setBrush( Qt::gray );
QGraphicsRectItem *innerRectItem = new QGraphicsRectItem( QRect( x+50, 50, 45, 100 ), rectItem, scene );
innerRectItem->setPen( QPen(Qt::black) );
innerRectItem->setBrush( Qt::white );
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( x+105, 50, 45, 100 ), rectItem, scene );
ellipseItem->setPen( QPen(Qt::black) );
ellipseItem->setBrush( Qt::white );
return rectItem;
}
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QGraphicsScene scene( QRect( 0, 00, 1000, 200 ) );
QGraphicsItem *item1 = createItem( 0, &scene );
QGraphicsItem *item2 = createItem( 200, &scene );
item2->translate( 300, 100 );
item2->rotate( 30 );
item2->translate( -300, -100 );
QGraphicsItem *item3 = createItem( 400, &scene );
item3->translate( 500, 100 );
item3->scale( 0.5, 0.7 );
item3->translate( -500, -100 );
QGraphicsItem *item4 = createItem( 600, &scene );
item4->translate( 700, 100 );
item4->shear( 0.1, 0.3 );
item4->translate( -700, -100 );
QGraphicsItem *item5 = createItem( 800, &scene );
item5->translate( 900, 100 );
item5->scale( 0.5, 0.7 );
item5->rotate( 30 );
item5->shear( 0.1, 0.3 );
item5->translate( -900, -100 );
QGraphicsView view;
view.setScene( &scene );
view.setViewport( new QGLWidget() );
view.show();
return app.exec();
}
I always got the error info " error: C2661: “QGraphicsRectItem::QGraphicsRectItem”: "No overloaded function takes 3 arguments".I tried again and again but all the same.Could somebody help me solving this problem? Thanks.
I am using Qt5.11.0 and MSVC2017 and Windows 10 pro X64.
The code you sample is inconsistent with Qt5, an updated translation is as follows:
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QOpenGLWidget>
static QGraphicsItem *createItem( int x, QGraphicsScene *scene )
{
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRectF( x+40, 40, 120, 120 ));
scene->addItem(rectItem);
rectItem->setPen(QPen(Qt::black));
rectItem->setBrush( Qt::gray );
QGraphicsRectItem *innerRectItem = new QGraphicsRectItem( QRect( x+50, 50, 45, 100 ), rectItem);
innerRectItem->setPen( QPen(Qt::black) );
innerRectItem->setBrush( Qt::white );
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( x+105, 50, 45, 100 ), rectItem);
ellipseItem->setPen( QPen(Qt::black) );
ellipseItem->setBrush( Qt::white );
return rectItem;
}
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QGraphicsScene scene( QRect( 0, 00, 1000, 200 ) );
QGraphicsItem *item1 = createItem( 0, &scene );
QGraphicsItem *item2 = createItem( 200, &scene );
QTransform tr2;
tr2.translate( 300, 100 );
tr2.rotate( 30 );
tr2.translate( -300, -100 );
item2->setTransform(tr2);
QGraphicsItem *item3 = createItem( 400, &scene );
QTransform tr3;
tr3.translate( 500, 100 );
tr3.scale( 0.5, 0.7 );
tr3.translate( -500, -100 );
item3->setTransform(tr3);
QGraphicsItem *item4 = createItem( 600, &scene );
QTransform tr4;
tr4.translate( 700, 100 );
tr4.shear( 0.1, 0.3 );
tr4.translate( -700, -100 );
item4->setTransform(tr4);
QGraphicsItem *item5 = createItem( 800, &scene );
QTransform tr5;
tr5.translate( 900, 100 );
tr5.scale( 0.5, 0.7 );
tr5.rotate( 30 );
tr5.shear( 0.1, 0.3 );
tr5.translate( -900, -100 );
item5->setTransform(tr4);
QGraphicsView view;
view.setScene( &scene );
view.setViewport( new QOpenGLWidget() );
view.show();
return app.exec();
}
*.pro
QT += core gui widgets opengl
TEMPLATE = app
CONFIG += c++11
SOURCES += main.cpp
It means exactly what it says, that you are trying to call the constructor of QGraphicsRectItem with three arguments:
... = new QGraphicsRectItem(QRect(x+40, 40, 120, 120), 0, scene);
\______________________/ | \___/
1 2 3
If you look at the documentation, you'll see that no such constructor exists:
QGraphicsRectItem(QGraphicsItem *parent = nullptr);
QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr);
The first has one optional argument (so zero or one), the second has one mandatory and one optional (so one or two) and the third has four mandatory and one optional (so four or five).
If you examine that previous paragraph closely, you'll notice that one thing missing is the word "three" :-) I'd suggest ditching that tutorial since it's very old. Qt 4.2 (when that class was first introduced) did have a three-argument version which included the scene, but that was very short-lived and removed in 4.3.
For 5.11, further reading of the linked documentation shows up the fact that the (my emphasis):
QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
Hence the correct way to do what you appear to need is:
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRectF(x+40, 40, 120, 120));
scene->addItem(rectItem);
In the included code I've created an application where I periodically update a label. When the application first starts up, updating the timeLabel results in redrawing the entire contents of the application. This can be observed by running the application with the --gtk-debug=updates argument.
When the button on the right side is clicked, the frame that encloses the contents of the window is removed from the widget hierarchy. This results in further updates to the timeLabel only redrawing the label, and not redrawing swapButton.
Why does a frame seem to want to redraw itself even if it doesn't need to?
#include <gtkmm.h>
class MyWindow
: public Gtk::Window
{
public:
MyWindow();
private:
bool timeout();
void toggleUseOfFrame();
Gtk::Frame frame;
Gtk::Label timeLabel;
Gtk::Button swapButton;
Gtk::Box box;
};
MyWindow::MyWindow()
{
// Layout widgets in initial configuration.
box.pack_start( timeLabel, true, true );
box.pack_start( swapButton, true, true );
box.set_homogeneous();
frame.add( box );
add( frame );
show_all();
set_size_request( 100, 50 );
// Setup signal handlers.
Glib::MainContext::get_default()->signal_timeout().connect(
sigc::mem_fun( *this, &MyWindow::timeout ), 1000 );
swapButton.signal_clicked().connect(
sigc::mem_fun( *this, &MyWindow::toggleUseOfFrame ) );
}
// Periodically update the label to force it to redraw.
bool MyWindow::timeout()
{
Glib::DateTime now = Glib::DateTime::create_now_local();
timeLabel.set_text( now.format( "%S" ) );
return true;
}
// If the frame is currently in use remove it. Otherwise add it back.
void MyWindow::toggleUseOfFrame()
{
if( frame.get_parent() ) {
remove();
box.reparent( *this );
}
else {
box.reparent( frame );
add( frame );
}
}
int main( int argc, char* argv[]) {
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create( argc, argv, "test" );
MyWindow myWindow;
return app->run( myWindow );
}
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.