I'm trying to align a label so that there is space on top and not on the bottom. What I want is about 30 between label1 and label2 but not between label2 and label3. I set label2 y padding to 30 and then on label2 y Gtk::ALIGN_BOTTOM, but it doesn't seem to work. Instead what I end up with is what looks like in the screenshot to be 30 on the top and 30 on the bottom of label2. I know there are ways around this but I want to know what is wrong with this code? I can't figure it out. I tried changing the pack options but that didn't work.
Here is the screenshot and here is the sample code:
int main( int argc, char *argv[] )
{
Gtk::Main kit( argc, argv );
Gtk::Window window;
window.set_default_size( 400, 400 );
Gtk::Label label1( "This is the first label." );
Gtk::Label label2( "This is the second label." );
Gtk::Label label3( "This is the third label." );
label2.set_padding( 0, 30 );
label2.set_alignment( Gtk::ALIGN_RIGHT, Gtk::ALIGN_BOTTOM );
Gtk::VBox vbox;
vbox.pack_start( label1, false, false, 0 );
vbox.pack_start( label2, false, false, 0 );
vbox.pack_start( label3, false, false, 0 );
window.add( vbox );
window.show_all_children();
Gtk::Main::run( window );
return 0;
}
Thanks
You'll need something like this (untested):
Gtk::Alignment alignment( 0.0, 1.0, 0.0, 0.0 );
alignment.set_padding( 30, 0, 0, 0 );
alignment.add( label2 );
The problem is that label.set_alignment() doesn't align within the padding, only within leftover space allocated to the widget.
Related
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:
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 );
}
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);
I'm trying to draw text on a widget at an angle which is non-perpendicular, i.e. between 0 and 90. Drawing the text itself is no issue, but the resulting text is very wiggly/unevenly drawn.
In the picture below, I'm drawing to lines of text at a 45 degree angle. The first line of text is many underscores ("_____"), and second line of text is "Multithreading". Underscores are drawn here instead of a line just to highlight the issue.
As you can see, the first line obviously shows the text is not evenly drawn. This is more subtle in the second line, but still visible.
Drawing at perpendicular angles (0, 90, 180, etc.) does not cause this effect. Any reason why this is happening?
I'm working on Windows 10 with Qt 5.7.0.
Minimal code example:
void MyWidget::paintEvent( QPaintEvent * /* event */ )
{
QFont font;
font.setPointSize( 16 );
font.setStyleStrategy( QFont::StyleStrategy::PreferAntialias );
setFont( font );
QImage image( size(), QImage::Format_ARGB32_Premultiplied );
QPainter imagePainter( &image );
imagePainter.initFrom( this );
imagePainter.setFont( font() );
imagePainter.setRenderHint( QPainter::Antialiasing, true );
imagePainter.eraseRect( rect() );
// Set logical origo in the middle of the image
m_window = QRect(
- width() / 2, // x
- height() / 2, // y
width(), // w
height() // h
);
imagePainter.setWindow( m_window );
m_viewport = QRect(
0, // x
0, // y
width(), // w
height() // h
);
imagePainter.setViewport( m_viewport );
draw( imagePainter );
imagePainter.end();
QPainter widgetPainter( this );
widgetPainter.drawImage( 0, 0, image );
}
void MyWidget::draw( QPainter & painter )
{
painter.save();
// Rotate anti-clockwise
painter.rotate( -m_degrees );
painter.drawText( m_window.top(), 0, tr( "Multithreads" ) );
painter.drawText( m_window.top(), 15, tr( "__________" ) );
painter.restore();
}
I found a workaround from this Qt bug ticket. In short, the fix is to draw the text as a QPainterPath rather than as text.
Example of the fix is
// Do this
QPainterPath glyphPath;
glyphPath.addText( x, y, painter.font(), text );
painter.fillPath( glyphPath, painter.pen().color() );
// instead of this
painter.drawText( x, y, text );
EDIT:
The difference can be seen below.
Before:
After:
I have recently started coding in C++ for the in-take assignment I need to make for my next school, and I'm seeing a big problem now and I don't know how to fix it. As soon as I try to run my code it said "Project.exe has stopped working". I tried to find solutions on the internet but I haven't found anything that solves my problem. This is my code (the SDL part are not the parts causing trouble, but I included them anyway for the completeness of the code):
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
class particle {
public:
void setxPos(int xx){
xP=xx;
};
void setyPos(int yy){
yP=yy;
};
void setxVel(int xv){
xVel=xv;
};
void setyVel(int yv){
yVel=yv;
};
int getxPos(){
return (xP);
};
int getyPos(){
return (yP);
};
protected:
int xP, yP;
int xVel = 0;
int yVel = 0;
};
void draw(SDL_Renderer* tempRend, particle drawJelly[6][4]){ //Function created for drawing the lines between the particles
SDL_RenderDrawLine( tempRend, drawJelly[1][1].getxPos(), drawJelly[1][1].getyPos(), drawJelly[2][1].getxPos(), drawJelly[2][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[2][1].getxPos(), drawJelly[2][1].getyPos(), drawJelly[3][1].getxPos(), drawJelly[3][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[3][1].getxPos(), drawJelly[3][1].getyPos(), drawJelly[4][1].getxPos(), drawJelly[4][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[4][1].getxPos(), drawJelly[4][1].getyPos(), drawJelly[5][1].getxPos(), drawJelly[5][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[5][1].getxPos(), drawJelly[5][1].getyPos(), drawJelly[6][1].getxPos(), drawJelly[6][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][1].getxPos(), drawJelly[6][1].getyPos(), drawJelly[6][2].getxPos(), drawJelly[6][2].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][2].getxPos(), drawJelly[6][2].getyPos(), drawJelly[6][3].getxPos(), drawJelly[6][3].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][3].getxPos(), drawJelly[6][3].getyPos(), drawJelly[6][4].getxPos(), drawJelly[6][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][4].getxPos(), drawJelly[6][4].getyPos(), drawJelly[5][4].getxPos(), drawJelly[5][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[5][4].getxPos(), drawJelly[5][4].getyPos(), drawJelly[4][4].getxPos(), drawJelly[4][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[4][4].getxPos(), drawJelly[4][4].getyPos(), drawJelly[3][4].getxPos(), drawJelly[3][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[3][4].getxPos(), drawJelly[3][4].getyPos(), drawJelly[2][4].getxPos(), drawJelly[2][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[2][4].getxPos(), drawJelly[2][4].getyPos(), drawJelly[1][4].getxPos(), drawJelly[1][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[1][4].getxPos(), drawJelly[1][4].getyPos(), drawJelly[1][3].getxPos(), drawJelly[1][3].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[1][3].getxPos(), drawJelly[1][3].getyPos(), drawJelly[1][2].getxPos(), drawJelly[1][2].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[1][2].getxPos(), drawJelly[1][2].getyPos(), drawJelly[1][1].getxPos(), drawJelly[1][1].getyPos() );
SDL_RenderPresent( tempRend );
SDL_Delay(100); //Wait .5 seconds for clarity of the drawing
}
void position(particle posJelly[6][4]){
int a=0;
while (a<6){ //Give all the jelly particles a position in the 20x20 grid
int b=0;
while(b<4){
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
b--;
}
a--;
}
}
int main( int argc, char* args[] )
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Surface* surface = NULL;
SDL_Init( SDL_INIT_VIDEO );
window = SDL_CreateWindow( "Jelly Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
renderer = SDL_CreateRenderer( window, -1, 0);
surface = SDL_GetWindowSurface( window );
SDL_SetRenderDrawColor( renderer, 0, 255, 0, 255 );
SDL_RenderPresent( renderer );
particle jelly[6][4]; //New object from class "particle" with name jelly
position(jelly); //Run function "position()" on particle jelly to set starting coördinates
bool inGame=true; //Start the game-loop
while(inGame){
draw(renderer, jelly);
inGame=false;
}
SDL_Delay( 2000 ); //Wait 2 seconds
SDL_DestroyWindow( window ); //Destroy the window "window"
SDL_Quit(); //Quit SDL
return 0; //End the program
}
Now, I already found where the cause lies, and that is in this part:
void position(particle posJelly[6][4]){
int a=0;
while (a<6){ //Give all the jelly particles a position in the 20x20 grid
int b=0;
while(b<4){
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
b--;
}
a--;
}
}
Because when I delete
position(jelly);
or remove
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
inside the position function, it doesn't give the error. (It doesnt work properly because it has no correct coordinates to draw from and to, but it 'works')
Again, I have searched in a lot of other articles and tutorials why this doesn't work or how this can be made work-able, but have found nothing yet.
The 'game' will be about a blob of jelly, made up out of 24 particles (6x4 but only the outer particles will be painted on the screen) that have a desirable distance to their neigbors, and then when they move closer or further away from eachother they will be drawn/pushed back to that distance, thus creating the 'bouncing' effect a blob of jelly has.
Please help!
Also, I know the Draw() function isn't very proffessional, but it's not going to be a full-release game anyway, and since the blob is always going to be 6 by 4, this is just a way easier way of drawing than finding the outer particles by loops and stuff.
You are trying to access the arrays with a negative index. You set b to 0 in the first iteration and than then you decrement it to make it -1. Then you use b in the next iteration the program goes boom. You probably meant to use ++ instead of --.
In this case the error has to do with negative values to array indexes.
You see an error like that when you program has raised an exeption and you don't handle it.
You should change:
a-- and b-- for a++ and b++
The first time your code reach the lines:
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
a==0 and b==0 but the second time (after a-- and b--) you would have something like this:
posJelly[-1][-1].setxPos((20*a)); // illegal C++ no negative inidices are allowed
posJelly[-1][-1].setyPos((20*b)); // illegal C++ no negative inidices are allowed
replace a decrementing operator with an increment: a-- and b-- with a++ andb++, respectively.