I'm trying to implement a simple KDE plasma plugin , which contains two PushButtons and a KLineEdit ,
But it seems KLineEdit and the PushButton has a minimum size , if the panel doesn't have enough height , some parts will disappear on the panel , it doens't shrink as the panel shrinks.
Since i already set the QSizePolicy to minimum , i don't understand why it's now resizing as user drag the panel ?
Thanks , and here's the primary class file:
#include <Plasma/Theme>
#include <Plasma/Corona>
#include <Plasma/Containment>
#include <Plasma/ToolTipManager>
K_EXPORT_PLASMA_APPLET(runcommand, RunCommandApplet)
RunCommandApplet:: RunCommandApplet(QObject *parent, const QVariantList &args) : Plasma::Applet(parent, args)
{
paste_button = new Plasma::PushButton (this);
paste_button->setText (tr("Pa"));
m_button = new Plasma::PushButton (this);
m_button->setText (tr("Ki"));
m_lineEdit = new Plasma::LineEdit (this);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout ();
layout->addItem (m_button);
layout->addItem (paste_button);
layout->addItem (m_lineEdit);
setLayout(layout);
constraintsEvent(Plasma::FormFactorConstraint | Plasma::ImmutableConstraint);
Plasma::ToolTipManager::self()->setContent(this, Plasma::ToolTipContent(i18n("Client"), i18n("Some Client"), KIcon("system-run").pixmap(IconSize(KIconLoader::Desktop))));
}
void RunCommandApplet::constraintsEvent(Plasma::Constraints constraints)
{
m_button->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
paste_button->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
m_lineEdit->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
}
Plasm::PushButtons QSizePolicy is Minimum by default. Try setting QSizePolicy::ShrinkFlag flag.
Related
On a user interface I have a QTableView that looks like this at the very beginning after first start:
As soon as the user run the project and create a new database .db (say the user puts the .db file on the Desktop), this QTableView looks like this which the correct behavior:
The problem: After I start saving images and their path all the headers carries different names (1,2,3,4) instead of (path1, path2, image1, image2) as shown previously and there is an id column missing, like this below which is the wrong behavior:
Below is how I am setting the most important parameters:
mainwindow.h
private:
QString temporaryFolder;
dataInfo *mNewDatabaseImages;
QSqlTableModel *mNewTableImages;
QStandardItemModel *model;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
temporaryFolder = "/home/path/toDesktop/folder/a.db";
QFile dbRem(temporaryFolder);
dbRem.remove();
mNewDatabaseImages = new dataInfo(this);
mNewDatabaseImages->initDataBase(temporaryFolder);
mNewDatabaseImages->confDataBase();
mNewTableImages = new QSqlTableModel(this, mNewDatabaseImages->getDatabase());
mNewTableImages->setTable("imageTable");
mNewTableImages->select();
ui->bookMarkTableView->setModel(mNewTableImages);
model = new QStandardItemModel();
ui->bookMarkTableView->setModel(model);
ui->bookMarkTableView->showColumn(true);
}
In addition to that there are two icons on the user interface with which I open an existing database or I create a new database as it is possible to see below:
This part is on the mainwindow.cpp too and below is the snipped of code:
void MainWindow::openDatabase(MainWindow::Opening opening)
{
QString nameDatabase;
if(opening == OPENING) {
nameDatabase = QFileDialog::getOpenFileName(this,
"Open Database", QDir::rootPath(),
"Database (*.db);;Any type (*.*)");
} else {
nameDatabase = QFileDialog::getSaveFileName(this,
"New Database", QDir::rootPath(),
"Database (*.db);;Any type (*.*)");
}
if(nameDatabase.isEmpty()) {
return;
}
if(!mNewDatabaseImages->initDataBase(nameDatabase)) {
QMessageBox::critical(this, "Error", mNewDatabaseImages->getError());
return;
}
if(!mNewDatabaseImages->confDataBase()) {
QMessageBox::critical(this, "Error", mNewDatabaseImages->getError());
return;
}
delete mNewTableImages;
// Work with the database file created
mNewTableImages = new QSqlTableModel(this, mNewDatabaseImages->getDatabase());
mNewTableImages->setTable("imageTable");
mNewTableImages->select();
ui->bookMarkTableView->setModel(mNewTableImages);
ui->bookMarkTableView->showColumn(true);
}
The database I am using is QSQLITE and the way the SQL is written is here if there is any need to see the snipped of that too.
Why does QTableView is not showing the correct headers and columns?
Thanks for pointing in the right direction.
That is because you are overwriting it with a new QStandardItemModel, therefore every time the program runs all the headers disappears. Try to modify your constructor by commenting out the last three lines in the following way:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
temporaryFolder = "/home/path/toDesktop/folder/a.db";
QFile dbRem(temporaryFolder);
dbRem.remove();
mNewDatabaseImages = new dataInfo(this);
mNewDatabaseImages->initDataBase(temporaryFolder);
mNewDatabaseImages->confDataBase();
mNewTableImages = new QSqlTableModel(this, mNewDatabaseImages->getDatabase());
mNewTableImages->setTable("imageTable");
mNewTableImages->select();
ui->bookMarkTableView->setModel(mNewTableImages);
// In this way you don't overwrite your initial headers
// model = new QStandardItemModel();
// ui->bookMarkTableView->setModel(model);
// ui->bookMarkTableView->showColumn(true);
}
I'm trying to create a simple pdf multi-page document with fields in it. To do that I have a template pdf which in the code I clone this template as many times as needed in order to create the document itself.
The problem comes with inserting some data in it. The type of data I try to insert to the document is not supposed to change across the pages. Rather than that, it stays static in all pages, Like the "Pages" digit that represents the number of the pages that this document contains.
Now, Inside my template pdf I have some text fields like, for instance, "Shipper1" and "Pages". I want to be able to insert my data into this text fields so that all the pages in the document will have this values in their "Shipper1" and "Pages" fields.
My code currently does that only on the first page. It shows the data perfectly. On the other hand, when I go to another page, the data isn't shown there. It's just displays an empty field.
Here is the code where I initiate the pdf document:
static void initiatePdf() {
// Initiate a new PDF Box object and get the acro form from it
File file = new File(Constants.Paths.EMPTY_DOC)
PDDocument tempDoc
Evaluator evaluator = new Evaluator(metaHolder)
int numPages = evaluator.getNumOfPagesRequired(objects)
FieldRenamer renamer = new FieldRenamer()
PDResources res = new PDResources()
COSDictionary acroFormDict = new COSDictionary()
List<PDField> fields = []
Closure isFieldExist = {List<PDField> elements, String fieldName ->
elements.findAll{it.getFullyQualifiedName() == fieldName}.size() > 0
}
for(int i = 0; i < numPages; i++) {
tempDoc = new PDDocument().load(file)
PDDocumentCatalog docCatalog = tempDoc.getDocumentCatalog()
PDAcroForm acroForm = docCatalog.acroForm
PDPage page = (PDPage) docCatalog.getPages().get(0)
renamer.setCurrentForm(acroForm)
if(i == 0) {
res = acroForm.getDefaultResources()
acroFormDict.mergeInto(acroForm.getCOSObject())
renamer.renameFields(1)
} else
renamer.renameFields(i*10+1)
List<PDField> newFields = acroForm.fields.findAll { PDField newField ->
isFieldExist(fields, newField.getFullyQualifiedName()) == false
}
fields.addAll(newFields)
document.addPage(page)
}
PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);
acroForm.setFields(fields)
acroForm.setDefaultResources(res);
document.documentCatalog.setAcroForm(acroForm)
}
A couple of things first:
metaHolder instance holds the information about all
the fields that reside inside the acro form. the info is: Field Name, Field Widget Width, Field Font and Font size
evaluator is just and instance of the Evaluator class. Its purpose is to analyze the dynamic data and decide how many pages will take to contain all that text data.
Here is where I try to populate the fields with text:
static void populateData() {
def properties = ["$Constants.Fields.SHIPPER" : "David"]
FieldPopulater populater = new FieldPopulater(document, metaHolder)
populater.populateStaticFields(properties)
}
FieldPopulater class:
package app.components
import app.StringUtils
import app.components.entities.DGObject
import app.components.entities.FieldMeta
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm
import org.apache.pdfbox.pdmodel.interactive.form.PDField
/**
* Created by David on 18/10/2016.
*/
class FieldPopulater {
PDAcroForm acroForm
FormMetaHolder metaHolder
FieldPopulater(PDDocument document, FormMetaHolder metaHolder) {
this.acroForm = document.getDocumentCatalog().acroForm
this.metaHolder = metaHolder
}
void populateStaticFields(properties) {
List<PDField> fields = []
properties.each {fieldName, data ->
FieldMeta fieldMeta = metaHolder.getMetaData(fieldName)
fields = acroForm.fields.findAll { PDField field ->
String currentName = field.getFullyQualifiedName()
char lastChar = currentName[-1]
if(Character.isDigit(lastChar)) {
currentName = currentName.substring(0,currentName.size()-1)
}
currentName == fieldName
}
if(fields.size() > 1) {
int counter = 1
String tempData = data
String currentFitData
while(tempData.isEmpty() != true) {
int maxWords = Utils.getMaxWords(tempData, fieldMeta)
currentFitData = StringUtils.getTextByWords(tempData, maxWords)
tempData = StringUtils.chopTextByWords(tempData, maxWords)
PDField field = fields.find{it.getFullyQualifiedName()[-1] == "$counter"}
field?.setValue(currentFitData)
counter++
}
} else {
PDField tempField = fields[0]
tempField.setValue(data)
}
}
}
}
The result is, in the first page, the field "Shipper" has a value of "David"
In the second page, the field "Shipper" is empty.
Here is an image. First page:
Second page:
What is the problem here?
UPDATE: I tried to add the widgets of every new acro form to the current page so that every field will a few kids widgets that will represent the field, but it still doesn't work.
// All the widgets that are associated with the fields
List<PDAnnotationWidget> widgets = acroForm.fields.collect {PDField field -> field.getWidgets().get(0)}
page.annotations.addAll(widgets)
UPDATE: I also tried to add the current widget of a field to the parent field's collection of widgets. Here is the code:
List<PDAnnotationWidget> widgets = []
// All the widgets that are associated with the fields
acroForm.fields.each {PDField field ->
PDAnnotationWidget widget = field.widgets.get(0)
// Adding the following widget to the page and to the field's list of annotation widgets
widgets.add(widget)
fields.find {it.getFullyQualifiedName() == field.getFullyQualifiedName()}?.widgets.add(widget)
}
page.annotations.addAll(widgets)
What you want is to have sereval visual representations of the same field. This is done by having several annotation widgets for such a field.
In PDF, when a field has only one annotation widget, they share a common dictionary. When it has several, the annotation widgets are in a child list of the field.
When you want several annotation widgets for one field, you need to create the annotation widgets with new PDAnnotationWidget() instead of calling field.getWidgets().get(0) and using that one. These widgets must be added to a list, and this list must be assigned to the field with setWidgets(). And for each widget you must call setRectangle() and setPage() and setParent().
An example for this is in the new CreateMultiWidgetsForm.java example. The setParent() method is not yet available in 2.0.3 (but will be in 2.0.4). In this answer, it is replaced with a call that does the same thing in a less elegant way.
public final class CreateMultiWidgetsForm
{
private CreateMultiWidgetsForm()
{
}
public static void main(String[] args) throws IOException
{
// Create a new document with 2 empty pages.
PDDocument document = new PDDocument();
PDPage page1 = new PDPage(PDRectangle.A4);
document.addPage(page1);
PDPage page2 = new PDPage(PDRectangle.A4);
document.addPage(page2);
// Adobe Acrobat uses Helvetica as a default font and
// stores that under the name '/Helv' in the resources dictionary
PDFont font = PDType1Font.HELVETICA;
PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), font);
// Add a new AcroForm and add that to the document
PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
// Add and set the resources and default appearance at the form level
acroForm.setDefaultResources(resources);
// Acrobat sets the font size on the form level to be
// auto sized as default. This is done by setting the font size to '0'
String defaultAppearanceString = "/Helv 0 Tf 0 g";
acroForm.setDefaultAppearance(defaultAppearanceString);
// Add a form field to the form.
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
// Acrobat sets the font size to 12 as default
// This is done by setting the font size to '12' on the
// field level.
// The text color is set to blue in this example.
// To use black, replace "0 0 1 rg" with "0 0 0 rg" or "0 g".
defaultAppearanceString = "/Helv 12 Tf 0 0 1 rg";
textBox.setDefaultAppearance(defaultAppearanceString);
// add the field to the AcroForm
acroForm.getFields().add(textBox);
// Specify 1st annotation associated with the field
PDAnnotationWidget widget1 = new PDAnnotationWidget();
PDRectangle rect = new PDRectangle(50, 750, 250, 50);
widget1.setRectangle(rect);
widget1.setPage(page1);
widget1.getCOSObject().setItem(COSName.PARENT, textBox);
// Specify 2nd annotation associated with the field
PDAnnotationWidget widget2 = new PDAnnotationWidget();
PDRectangle rect2 = new PDRectangle(200, 650, 100, 50);
widget2.setRectangle(rect2);
widget2.setPage(page2);
widget2.getCOSObject().setItem(COSName.PARENT, textBox);
// set green border and yellow background for 1st widget
// if you prefer defaults, just delete this code block
PDAppearanceCharacteristicsDictionary fieldAppearance1
= new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance1.setBorderColour(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE));
fieldAppearance1.setBackground(new PDColor(new float[]{1,1,0}, PDDeviceRGB.INSTANCE));
widget1.setAppearanceCharacteristics(fieldAppearance1);
// set red border and green background for 2nd widget
// if you prefer defaults, just delete this code block
PDAppearanceCharacteristicsDictionary fieldAppearance2
= new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance2.setBorderColour(new PDColor(new float[]{1,0,0}, PDDeviceRGB.INSTANCE));
fieldAppearance2.setBackground(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE));
widget2.setAppearanceCharacteristics(fieldAppearance2);
List <PDAnnotationWidget> widgets = new ArrayList<PDAnnotationWidget>();
widgets.add(widget1);
widgets.add(widget2);
textBox.setWidgets(widgets);
// make sure the annotations are visible on screen and paper
widget1.setPrinted(true);
widget2.setPrinted(true);
// Add the annotations to the pages
page1.getAnnotations().add(widget1);
page2.getAnnotations().add(widget2);
// set the field value
textBox.setValue("Sample field");
document.save("MultiWidgetsForm.pdf");
document.close();
}
}
I am trying to generate the barcode from barcode4j library(code128bean, other barcode beans) and try to add to the existing pdf. The barcode image is getting created locally using the below code.
//Create the barcode bean
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
//width exactly one pixel
//bean.setCodeset(2);
code128Bean.doQuietZone(false);
//Open output file
File outputFile = new File("D:/barcode4jcod128.png"); //I dont want to create it
OutputStream code128Stream = new FileOutputStream(outputFile);
try {
//Set up the canvas provider for monochrome PNG output
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
code128Stream, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
code128Bean.generateBarcode(canvas1, "123456");
//Signal end of generation
canvas1.finish();
} finally {
code128Stream.close();
}
My problem is I don't want to create an image and save it locally in filesystem and then add it as image to pdf. I just want to create dynamically i mean just create the barcode image dynamically and add it to the pdf.
How do I set the pagesize (like PDPage.PAGE_SIZE_A4) to the existing PDPages which I retrieved from catalog.getAllPages() method, like (List<PDPage> pages = catalog.getAllPages();)
Can somebody help on this?
Thank you so much for your help Tilman. Here is what i did
public static BufferedImage geBufferedImageForCode128Bean(String barcodeString) {
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
code128Bean.doQuietZone(false);
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
//Generate the barcode
code128Bean.generateBarcode(canvas1, barcodeString);
return canvas1.getBufferedImage();
}
// main code
PDDocument finalDoc = new PDDocument();
BufferedImage bufferedImage = geBufferedImageForCode128Bean("12345");
PDXObjectImage pdImage = new PDPixelMap(doc, bufferedImage);
PDPageContentStream contentStream = new PDPageContentStream(
finalDoc, pdPage, true, true, true
);
contentStream.drawXObject(pdImage, 100, 600, 50, 20);
contentStream.close();
finalDoc.addPage(pdPage);
finalDoc.save(new File("D:/Test75.pdf"));
The barcode is getting created the but it is created in vertical manner. i would like to see in horizontal manner. Thanks again for your help.
1) add an image to an existing page while keeping the content:
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
code128Bean.generateBarcode(canvas1, "123456");
canvas1.finish();
BufferedImage bim = canvas1.getBufferedImage();
PDXObjectImage img = new PDPixelMap(doc, bim);
PDPageContentStream contents = new PDPageContentStream(doc, page, true, true, true);
contents.drawXObject(img, 100, 600, bim.getWidth(), bim.getHeight());
contents.close();
2) set the media box to A4 on an existing page:
page.setMediaBox(PDPage.PAGE_SIZE_A4);
I am trying to make a custom widget: for displaying a processor register which has a name, a value and can be displayed in octal/decimal hexa. The code is shown at the bottom. I receive better result when I use the code as shown (i.e I insert QRadioButtons):
If I use
mainLayout->addWidget(DisplayMode);
instead (I guess this is the correct method) then the resulting picture is
Do I misunderstand something? What is wrong?
RegisterWidget::RegisterWidget(QWidget *parent)
:QFrame (parent)
{
mValue = 0;
mName = "";
setFrameStyle(QFrame::Panel | QFrame::Sunken);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
label = new QLabel(tr("mName"),this);
label->setText(mName);
label->setLineWidth(2);
QGroupBox *DisplayMode = new QGroupBox("");
QRadioButton *OctalR = new QRadioButton(this);
QRadioButton *DecimalR = new QRadioButton(this);
DecimalR->setChecked(true); DecimalR->setDown(true);
QRadioButton *HexaR = new QRadioButton(this);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(OctalR);
hbox->addWidget(DecimalR);
hbox->addWidget(HexaR);
hbox->addStretch(1);
DisplayMode->setLayout(hbox);
mainLayout->addWidget(label);
Value = new QLCDNumber(this);
Value->setDigitCount(8);
Value->setSegmentStyle(QLCDNumber::Flat);
Value->display(mValue);
mainLayout->addWidget(Value);
/* mainLayout->addWidget(DisplayMode);*/
mainLayout->addWidget(OctalR);
mainLayout->addWidget(DecimalR);
mainLayout->addWidget(HexaR);
setLineWidth(3);
setLayout(mainLayout);
connect(OctalR, SIGNAL(clicked()), this, SLOT(setOctal()));
connect(DecimalR, SIGNAL(clicked()), this, SLOT(setDecimal()));
connect(HexaR, SIGNAL(clicked()), this, SLOT(setHexa()));
}
Call QLayout::setContentsMargins() for both mainLayout and hbox. Try (3, 3, 3, 3) as parameters for a starting point and tweak. Layouts have default margins of 11 pixels on most platforms, according to the docs.
I have a basic window (frame) (made with FormBuilder 3.5 actually) with a a vertical sizer and then a horizontal one(shown in code). In the H sizer I have a full ribbon with 1 (page, panel, buttonBar) and one button. I am adding a panel to the ribbon page and then adding a button to the panel. It is being sized so small that the button ends up in a drop-box(pane?) with the button in it. There should be no size constraint, it has plenty of space to be a button.
//late creation code
wxRibbonPage *ribbonPage = m_mainFrame->m_mainRibbonPage;//public pointer
if (nullptr != ribbonPage)
{
wxBoxSizer *a = new wxBoxSizer(wxHORIZONTAL);
wxRibbonPanel *panel = new wxRibbonPanel(ribbonPage, wxID_ANY, wxT("button group"), wxNullBitmap, wxDefaultPosition, wxSize(-1, -1), wxRIBBON_PANEL_DEFAULT_STYLE);
panel->SetSizer(a);
panel->SetSize(wxSize(300, -1));//looks same with/without
wxRibbonButtonBar *btnBar = new wxRibbonButtonBar(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0);
btnBar->AddToggleButton(wxID_ANY, wxT("play"), wxArtProvider::GetBitmap(wxART_MISSING_IMAGE, wxART_TOOLBAR), wxEmptyString);
}
m_mainFrame->m_ribbonBar2->Realize();
m_mainFrame->m_ribbonBar2->Layout();
m_mainFrame->Bind(wxEVT_MENU, &MyApp::OnSelectCard, this);//these easy
m_mainFrame->Show();
-Having a panel title or not doesn't seem to help.
-Calling wxButtonBar->Realize() doesn't seem to help.
-Calling wxRibbonBar->Layout() doesn't seem to help.
-Setting the minimum panel width to 300 appears to have NO affect.
-Creating a H box sizer and using newPanel->SetSizer(wxBoxSizer a) nope.
-Setting the panel size after setting the box sizer, nope.
and as an added benefit, the panel width goes to about 0 with the sizer.
//early object code derived from wxForm
wxBoxSizer* bSizer26;
bSizer26 = new wxBoxSizer( wxHORIZONTAL );
m_ribbonBar2 = new wxRibbonBar( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxRIBBON_BAR_DEFAULT_STYLE );
m_ribbonBar2->SetArtProvider(new wxRibbonDefaultArtProvider);
m_mainRibbonPage = new wxRibbonPage( m_ribbonBar2, ID_RIBBON_PAGE, wxEmptyString , wxNullBitmap , 0 );
m_ribbonBar2->SetActivePage( m_mainRibbonPage );
m_ribbonPanel2 = new wxRibbonPanel( m_mainRibbonPage, wxID_ANY, wxEmptyString , wxNullBitmap , wxDefaultPosition, wxDefaultSize, wxRIBBON_PANEL_DEFAULT_STYLE );
m_ribbonPanel2->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) );
m_ribbonButtonBar2 = new wxRibbonButtonBar( m_ribbonPanel2, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
m_ribbonButtonBar2->AddHybridButton( wxID_ANY, wxT("Card Select"), wxArtProvider::GetBitmap( wxART_MISSING_IMAGE, wxART_TOOLBAR ), wxEmptyString);
m_ribbonBar2->Realize();
bSizer26->Add( m_ribbonBar2, 1, wxALL, 0 );
I have been looking at wxwidgets.org.
I have been referencing the samples.
I have looked for similar questions.
If I get linked(in comments) to a resource that has an answer, I'll compose an answer if there isn't one.
The reason this happens is because the ribbon auto-sizes your button to a size that's smaller the auto size of the panel it's contained in. If you add at least two buttons to a panel then it looks fine.
But fear not, there is a way around this. I had to crack open the source code to figure it out, so it might be a bit of a hack. I am not sure if the developers plan on exposing the functionality or not.
The buttonbar.h header does not expose the members of wxRibbonButtonBarButtonSizeInfo or wxRibbonButtonBarButtonBase, so first you have to declare them in your source:
class wxRibbonButtonBarButtonSizeInfo
{
public:
bool is_supported;
wxSize size;
wxRect normal_region;
wxRect dropdown_region;
};
class wxRibbonButtonBarButtonBase
{
public:
wxRibbonButtonBarButtonInstance NewInstance();
wxRibbonButtonBarButtonState GetLargestSize();
bool GetSmallerSize(wxRibbonButtonBarButtonState* size, int n = 1);
wxString label;
wxString help_string;
wxBitmap bitmap_large;
wxBitmap bitmap_large_disabled;
wxBitmap bitmap_small;
wxBitmap bitmap_small_disabled;
wxRibbonButtonBarButtonSizeInfo sizes[3];
wxClientDataContainer client_data;
int id;
wxRibbonButtonKind kind;
long state;
};
Now that you have these members declared, you can simply change the "x" value of the "LARGE" size of the button that you added to the bar:
wxRibbonButtonBarButtonBase* newButton = buttonBar->AddButton( wxID_NEW,
"New", wxArtProvider::GetBitmap(wxART_NEW, wxART_TOOLBAR) );
newButton->sizes[wxRIBBON_BUTTONBAR_BUTTON_LARGE].size.x = 48;
And viola! Your single button, now having the correct width, will be displayed correctly on the panel. It seems that 48 pixels is the correct minimum size.
Also, you don't need a horizontal sizer to contain the ribbon. I typically just stack a ribbon and panel in a vertical sizer and then use the panel for the "main" part of the window.
wxFrame* frame = new wxFrame( NULL, wxID_ANY, "wxTestProject",
wxDefaultPosition, wxSize(640,480) );
wxRibbonBar* ribbon = new wxRibbonBar( frame, wxID_ANY );
wxPanel* panel = new wxPanel( frame );
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
sizer->Add( ribbon, 0, wxGROW, 0 );
sizer->Add( panel, 1, wxGROW, 0 );
frame->SetSizer( sizer );