I am writing a Gtk application and I am having troubles centering some buttons.
I created a popup window in this way:
dialog = gtk_dialog_new_with_buttons ("Add element",
GTK_WINDOW(gElems->window),
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
"OK",
GTK_RESPONSE_ACCEPT,
"Cancel",
GTK_RESPONSE_REJECT,
NULL);
Everything is ok, except for the OK and Cancel buttons, which are aligned to the right end of the popup window, while I would like them to be centered (as you can see from the picture).
Is there a way to do so without using additional containers to store the buttons?
You can align the buttons in the middle
buttons_container = gtk_dialog_get_action_area (dialog)
g_object_set_property (G_OBJECT (buttons_container), "halign", GTK_ALIGN_CENTER)
and voila.....
If you want to change the container, you also can, but that's not what you want, really
Regarding luciomrx answer: It is needed to pass a gvalue, otherwise you will get a compile warning and a runtime crash:
GValue val = G_VALUE_INIT;
g_value_init (&val, G_TYPE_ENUM);
g_value_set_enum (&val, GTK_ALIGN_CENTER);
buttons_container = gtk_dialog_get_action_area (dialog);
g_object_set_property (G_OBJECT (gtk_dialog_get_action_area (dialog)), "halign", &val);
Even Simpler, you can just use gtk_widget_set_halign, which accepts an enum:
gtk_widget_set_halign (gtk_dialog_get_action_area (dialog), GTK_ALIGN_CENTER);
Edit: I just realized that gtk_dialog_get_action_area anyhow is deprecated in gtk3 .. so a different sulution should be found.
Related
My GTK# (v2.12.44) app has a single Window with a single button on it. The button appears to be centered on the Window, because that's where the text appears, but the border/background for the button is shifted up and to the left for no reason I can see. Has anyone seen this before? I saw similar behavior from an Image widget. Note this only happens in Windows, not on (for example) Raspberry Pi/Jesse.
Here's the code:
Application.Init();
var window = new Window("test") {new Button("testing 1, 2, 3")};
window.Maximize();
window.ShowAll();
Application.Run();
Your code works well. Try to add a container before adding a button and set some parametrs you want to. Fill and Expand when adding Button.
I have a QDialog subclass containing some options of my application. Some of the options are core, the other are advanced, so I decided to put them into separeted checkable QGroupBox.
I want my dialog to shrink verticaly when the user checked off advanced options box, but I can't find the way to do it properly - the dialog size stays exactle the same
I set dialog's size policy to Expanding, tried to call adjustSize() and tried to call resize() method - nothing helps. I can't resize programmaticaly dialog to be smaller then it's current size (it only can become larger). At the same time, it is possible to resize it manualy.
Can anybody help me?
If you don't need manual resize, you can add
layout()->setSizeConstraint(QLayout::SetFixedSize);
to the dialog constructor, then the layout takes over the responsibility to automatically resize when widgets are shown or hidden.
I'm creating a context menu for certain elements using a PopupPanel; the menu itself is going to be fairly large and complex. What I'm looking to do is to have a list of buttons, plus an image and some text, related to the element clicked.
My problem is that I'd like the buttons to always display directly under the clicked element, because that's convenient for the user; the issue is that when PopupPanel is near the edges of the screen, it automatically changes position to be fully visible, not aligning its left side to the element as usual. I like this behavior, but it moves the position of the buttons away.
So what I'd like to happen is: normally the buttons are on the left of the panel, the other stuff is to the right. When the panel is close to the right of the screen, I'd like the buttons to instead be on the right (and thus under the clicked element) and the other stuff on the left.
Is there a clever way to do this, either in GWT or better yet, using only CSS? PopupPanel itself doesn't seem to tell you when it's going to get flipped, sadly. The only solution I currently have is to manually check the position and width of the popup before showing it and adjust accordingly, but I'm hoping there's a better solution.
Here is what I suggest (based on my own implementation of a similar use case):
Have the position callback implementation accept references (in constructor) on:
PopupPanel element
element on which user right cliked
the content you put in the PopupPanel
Replicate (I know this not DRY but implementation is package private) the code from default position callback
When opening to the right invoke a method that changes the layout of your content (CSS based or otherwise)
I hope it helps. If you find something better let me know.
Using code (not the Interface builder) I need to create an NSButton that looks like an image. Specifically I want to use NSImageNameStopProgressFreestandingTemplate and I need it not to look like button but to look like the image. This means:
1. No 'button down' look
2. No border, no any visibility of the button
Thanks.
I know this response is a bit late, but you could try this, given thisButton:
[thisButton setImage:[NSImage imageNamed:NSImageNameStopProgressFreestandingTemplate]];
[thisButton setImagePosition:NSImageOnly];
[thisButton setBordered:NO];
That last line is the key bit: removing the button border effectively strips it of its bezel, leaving only the image to click on. (BTW, I haven't tried the above code specifically, so you may need to throw in a couple of other tweaks, such as setting the imageScaling or buttonType, to get it to work best.)
One final note: If you're using a template image (as you said you would), Cocoa will automatically display it with a slight dark-grey gradient; when the button is clicked, it will momentarily darken to solid black. This is an automatic "'button down' look" you didn't want; however, it is very subtle, and is a good indicator that the button worked. If you don't want this to happen, you could get an instance of the desired image and [stopImage setTemplate:NO]; on it.
Disable isBordered
let button = NSButton(
image: NSImage(named: NSImage.Name("plus"))!,
target: self,
action: #selector(onButtonPress)
)
button.isBordered = false
If you don't want to use a templated but want the push down highlight anyways, you can also use the following setup for an NSButton:
let imageButton = NSButton()
imageButton.image = NSImage(named: "MyImage")!
imageButton.bezelStyle = .shadowlessSquare
imageButton.isBordered = false
imageButton.imagePosition = .imageOnly
The important thing to make the highlight work on any image is to set bezelStyle to shadowlessSquare.
I know this behavior wasn't requested in the question, but it might be useful for others.
I have a Qt dialog application. Now I dont want that dialog to be resizeable. I am not sure how to achieve this. I tried a bunch of things but still when the dialog launches this dialog can be resized.
What is the property that i should set to disable the dialog/Widget resize.
I also tried
setSizePolicy(QSizePolicy::Fixed);
But i get an error saying..
source\nimcac_settingsMain.cpp(36) : error C2248:
**'QSizePolicy::QSizePolicy' : cannot access private member declared in class 'QSizePolicy'**
p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui\../../src\gui\
kernel\qsizepolicy.h(177) : see declaration of 'QSizePolicy::QSizePolicy'
p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui\../../src\gui\
kernel\qsizepolicy.h(34) : see declaration of 'QSizePolicy'
Kindly help me out with this.
The compile error you get is because you try to pass a QSizePolicy::Policy to setSizePolicy(QSizePolicy), but there's no implicit conversion from QSizePolicy::Policy (which is the policy for one dimension) to QSizePolicy (which is a class containing, among other things, one Policy per dimension (height, width)). QSizePolicy doesn't work on top-level widgets (windows) anyway, though.
setFixedSize() only works if you know the size of the dialog in advance (and usually you don't, what with changing font sizes and languages). You can do
window()->setFixedSize( window()->sizeHint() );
but it's much better to use
window->layout()->setSizeConstraint( QLayout::SetFixedSize );
That lets the layout determine the size of the dialog, but doesn't allow resizing, which I assume is what you were asking for.
I don't know if you already tried it, but QWidget::setFixedSize should do what you want
this->setFixedSize(this->width(),this->height());
You need to change the windowFlags of the dialog and set it to Qt::MSWindowsFixedSizeDialogHint.
This only works in windows.
For more information please see this example:
http://doc.qt.digia.com/4.5/widgets-windowflags.html
On QT Creator, in the UI editor, click on the top object in the properties window, then scroll at the bottom in the Layout part. You should see the layoutSizeConstraint property.
Set the layoutSizeConstraint to SetFixedSize.
If you use QtCreator (of course you are) you can set the property HorizontalsizePolicy to fixed and Vertical Policy also to Fixed. Then you can set the maximumSize to the dimensions you want. The window will not maximise again.
In code you can do something like this,
Dialog->resize(581, 292);
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(WaterLevelEditorDialog->sizePolicy().hasHeightForWidth());
Dialog->setSizePolicy(sizePolicy);
Dialog->setMinimumSize(QSize(581, 292));
Dialog->setMaximumSize(QSize(581, 292));
Dialog->setSizeGripEnabled(false);
In QtCreator do as follows,
Select the dialog box widget
Locate the dialog box widget in Object Window
In Object Window, right click on dialog box object to popup a menu
Select "Size Constraints" -> "Set Maximum Size" from the menu
Right click again on dialog object to popup a menu
Select "Size Constraints" -> "Set Minimum Size"
In Property Window,
ensure "sizePolicy"->"Horizontal Policy" has value "Fixed"
ensure "sizePolicy"->"Vertical Policy" has value "Fixed"
ensure "sizeGripEnabled" is not checked
From the Qt documentation, setSizePolicy() method either takes zero argument or two arguments but cannot be one argument. That's why you get this compilation error. From my experiment, if you don't set the fixed size. This method has no use. The window can still be resizable.
An easier way is to set the maxium size to 0.
this->setMaxiumSize(QSize(0, 0));
In case you are designing UI in QML and launching using QDeclarativeView, try the below code.
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
.
.
.
//To make the window non-resizable
viewer->setFixedSize(viewer->width(),viewer->height());
Here QmlApplicationViewer is derived from QDeclarativeView.